复制代码代码如下:
paket com.lkb.test;
impor java.util.arraylist;
impor java.util.hashmap;
impor java.util.iterator;
impor java.util.list;
impor java.util.map;
impor org.apache.http.httpresponse;
impor org.apache.http.client.responshandler;
impor org.apache.http.client.entity.urlencodedformentity;
impor org.apache.http.client.methods.httpget;
impor org.apache.http.client.methods.httppost;
impor org.apache.http.impl.client.basicResponseHandler;
impor org.apache.http.impl.client.defaulthttpClient;
impor org.apache.http.message.basicnamevaluePair;
impor org.apache.http.message.bufferedheader;
impor org.apache.http.protocol.http;
impor com.util.constant;
kelas publik jd {
// item konfigurasi
Private Static String redirecturl = "http://order.jd.com/center/list.action";
Private Static String LoginUrl = "http://passport.jd.com/uc/login";
// Jangan ubah URL berikut
string statis pribadi renrenloginurl = "https://passport.jd.com/uc/loginservice";
// httpClient digunakan dalam satu sesi
respons httpresponse pribadi;
Private DEFAULTHTTPCLIENT httpClient = new DEFAULTHTTPCLIENT ();
peta publik <string, string> getParams () {
Peta <string, string> peta = new HashMap <String, String> ();
String str = getText (loginUrl);
String strs1 [] = str.split ("name =/" uuid/"value =/" ");
String strs2 [] = strs1 [1] .split ("/"/> ");
String uuid = strs2 [0];
peta.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 ("/" ");
Tombol string = strs5 [0];
Nilai string = strs5 [2];
peta.put (tombol, nilai);
peta mengembalikan;
}
Private Boolean Login () {
Peta peta = getParams ();
Httppost httpost = httpppost baru (renrenloginurl);
// Semua parameter memposting ke situs web
Daftar <TomennamevaluePair> nvps = new ArrayList <TomennamevaluePair> ();
nvps.add (basicnamevaluePair baru ("returnurl", redirecturl));
nvps.add (BasicNamevaluePair baru ("LoginName", Constant.username));
nvps.add (basicNamevaluePair baru ("nloginpwd", constant.password));
nvps.add (basicnamevaluePair baru ("loginpwd", constant.password));
Iterator it = map.keyset (). Iterator ();
while (it.hasnext ()) {
String key = it.next (). ToString ();
Nilai string = Map.get (key) .toString ();
nvps.add (basicNamevaluePair baru (kunci, nilai));
}
mencoba {
httpost.setEntity (UrlencodedFormentity baru ((Daftar <? Extends org.apache.http.namevaluepair>) nvps, http.utf_8));
respons = httpclient.execute (httpost);
} catch (Exception e) {
e.printstacktrace ();
mengembalikan false;
} Akhirnya {
httpost.abort ();
}
Kembali Benar;
}
Private String getRedirectLocation () {
BufferedHeader LocationHeader = (bufferedHeader) response.getFirStheader ("Lokasi");
if (locationHeader == null) {
kembali nol;
}
return locationHeader.getValue ();
}
Private String getText (string redirectlocation) {
Httpget httpget = httpget baru (redirectlocation);
ResponseHandler <String> responseHandler = baseResponseHandler baru ();
String responsebody = "";
mencoba {
responseBody = httpclient.execute (httpget, responseHandler);
} catch (Exception e) {
e.printstacktrace ();
responsbody = null;
} Akhirnya {
httpget.abort ();
//httpclient.getConnectionManager (). shutdown ();
}
mengembalikan responsbody;
}
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 = JD baru ();
//renren.getParams ();
renren.printtext ();
}
}