复制代码代码如下:
пакет com.lkb.test;
импортировать java.util.arraylist;
импортировать java.util.hashmap;
импортировать java.util.iterator;
импортировать java.util.list;
импортировать java.util.map;
Импорт org.apache.http.httpresponse;
Импорт org.apache.http.client.responsehandler;
Импорт org.apache.http.client.entity.urlencodedFormentity;
Импорт org.apache.http.client.methods.httpget;
Импорт org.apache.http.client.methods.httppost;
Импорт org.apache.http.impl.client.basicresponsehandler;
Импорт org.apache.http.impl.client.defaulthttpclient;
Импорт org.apache.http.message.basicnamevaluepair;
Импорт org.apache.http.message.bufferedheader;
Импорт org.apache.http.protocol.http;
импорт com.util.constant;
открытый класс JD {
// элементы конфигурации
Private Static String redirecturl = "http://order.jd.com/center/list.action";
Private Static String LoginUrl = "http://passport.jd.com/uc/login";
// не меняйте следующий URL
Частная статическая строка renrenloginurl = "https://passport.jd.com/uc/loginservice";
// httpclient используется в одном сеансе
частный ответ httpresponse;
Частный Defaulthttpclient httpclient = new Defaulthttpclient ();
public Map <String, String> getParams () {
Map <string, string> map = new hashmap <string, string> ();
String str = getText (loginurl);
String strs1 [] = str.split ("name =/" uuid/"value =/" ");
String strs2 [] = strs1 [1] .split ("/"/> ");
String uuid = strs2 [0];
map.put ("uuid", uuid);
System.out.println (strs2 [0]);
String str3s [] = strs1 [1] .split ("<span class =/" clr/"> </span> <input type =/" hidden/"name =/" ");
String strs4 [] = str3s [1] .split ("/>");
String strs5 [] = strs4 [0] .trim (). Split ("/" ");
String Key = strs5 [0];
String value = strs5 [2];
map.put (ключ, значение);
карта возврата;
}
Private Boolean Login () {
Карта карта = getParams ();
Httppost httpost = new httppost (renrenloginurl);
// Все параметры публикуются на веб -сайте
List <BasticNameValuePair> nvps = new ArrayList <BasicnameValuePair> ();
nvps.add (new basicnamevaluepair ("returnurl", redirecturl));
nvps.add (new basicnamevaluepair ("loginname", constant.username));
nvps.add (new basicnamevaluepair ("nloginpwd", constant.password));
nvps.add (new basicnamevaluepair ("loginpwd", constant.password));
Итератор it = map.keyset (). Iterator ();
while (it.hasnext ()) {
String Key = it.next (). ToString ();
String value = map.get (key) .toString ();
nvps.add (new basicnamevaluepair (ключ, значение));
}
пытаться {
httpost.setentity (new urlencodedformentity ((list <? extends org.apache.http.namevaluepair>) nvps, http.utf_8));
response = httpclient.execute (httpost);
} catch (Exception e) {
e.printstacktrace ();
вернуть ложь;
} окончательно {
httpost.abort ();
}
вернуть истину;
}
частная строка getredirectlocation () {
BufferedHeader locationHeader = (BufferedHeader) response.getFirStheader ("location");
if (locationHeader == null) {
вернуть ноль;
}
return locationHeader.getValue ();
}
Private String getText (string redirectlocation) {
Httpget httpget = new httpget (redirectlocation);
Responsehandler <string> responsehandler = new BasicResponseHandler ();
Строка ответаbody = "";
пытаться {
responsebody = httpclient.execute (httpget, responsehandler);
} catch (Exception e) {
e.printstacktrace ();
responsebode = null;
} окончательно {
httpget.abort ();
//httpclient.getConnectionManager (). Shutdown ();
}
возвратный ответ;
}
public void printtext () {
if (login ()) {
System.out.println (getText (redirecturl));
String redirectlocation = getRedirectlocation ();
if (redirectlocation! = null) {
System.out.println (getText (redirectlocation));
}
}
}
public static void main (string [] args) {
Jd renren = new jd ();
//renren.getParams ();
renren.printtext ();
}
}