复制代码代码如下:
package com.lkb.test;
import java.util.arraylist;
import java.util.hashmap;
Importer java.util.iterator;
Importer java.util.list;
importation java.util.map;
import org.apache.http.httpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.urLencocedFormentity;
import org.apache.http.client.methods.httpget;
import org.apache.http.client.methods.httppost;
import org.apache.http.impl.client.basicResponseHandler;
import org.apache.http.impl.client.defaulthTTPClient;
import org.apache.http.message.basicNameValuepair;
import org.apache.http.message.bufferedHeader;
import org.apache.http.protocol.http;
import com.util.constant;
classe publique JD {
// les éléments de configuration
STATIQUE STATIQUE PRIVATE redirectUrl = "http://order.jd.com/center/list.action";
chaîne statique privée Loginurl = "http://passport.jd.com/uc/login";
// ne change pas l'URL suivante
chaîne statique privée Renrenloginurl = "https://passport.jd.com/uc/loginservice";
// le httpclient est utilisé en une seule session
Réponse privée HTTPResponse;
DefaulthTTPClient privé httpClient = new defaulthTTPClient ();
Map public <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 (clé, valeur);
carte de retour;
}
Login booléen privé () {
Map map = getParams ();
HTTPPOST HTTPOST = NOUVEAU HTTPPOST (Renrenloginurl);
// Tous les paramètres publient sur le site Web
List <BasicNameValuePair> 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));
Iterator it = map.KeySet (). Iterator ();
while (it.hasnext ()) {
String key = it.next (). ToString ();
String value = map.get (key) .toString ();
nvps.add (nouveau BasicNameValuePair (clé, valeur));
}
essayer {
httpost.SetEntity (new urlencodedFormentity ((list <? étend org.apache.http.nameValuepair>) nvps, http.utf_8));
réponse = httpclient.exécute (httPost);
} catch (exception e) {
e.printStackTrace ();
retourne false;
} enfin {
httpost.abort ();
}
Retour Vrai;
}
chaîne privée getRedirectLocation () {
BufferedHeader LocationHeader = (BufferedHeader) Response.getFiRStheader ("emplacement");
if (emplacementheader == null) {
retourner null;
}
return locationheader.getValue ();
}
String privé getText (String redirectLocation) {
Httpget httpget = new httpget (redirectLocation);
RéponseHandler <string> ResponseHandler = new BasicResponseHandler ();
String réponsebody = "";
essayer {
ResponseBody = httpClient.Execute (httpget, réponsehandler);
} catch (exception e) {
e.printStackTrace ();
ResponseBody = null;
} enfin {
httpget.abort ();
//httpclient.getConnectionManager (). Shutdown ();
}
Retour ResponseBody;
}
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 ();
}
}