复制代码代码如下:
パッケージ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をインポートします。
import org.apache.http.impl.client.basicResponseHandler;
import 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を変更しないでください
private static string renrenloginurl = "https://passport.jd.com/uc/loginservice";
// HTTPCLIENTは1つのセッションで使用されます
プライベートHTTPResponse応答。
private defaulthtpclient httpclient = new defulthttpclient();
パブリックマップ<文字列、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( "/"/> ");
文字列uuid = strs2 [0];
map.put( "uuid"、uuid);
System.out.println(strs2 [0]);
string str3s [] = strs1 [1] .split( "<span class =/" clr/"> </span> <入力タイプ=/" hidden/"name =/" ");
string strs4 [] = str3s [1] .split( "/>");
string strs5 [] = strs4 [0] .trim()。split( "/" ");
文字列key = strs5 [0];
文字列値= strs5 [2];
map.put(key、value);
マップを返します。
}
private boolean login(){
マップマップ= getParams();
httppost httpost = new httppost(renrenloginurl);
//すべてのパラメーターはWebサイトに投稿します
リスト<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(new BasicNameValuePair(key、value));
}
試す {
httpost.setentity(new UrlencodedFormentity((list <?extends org.apache.http.namevaluepair>)nvps、http.utf_8));
応答= httpclient.execute(httpost);
} catch(例外e){
e.printstacktrace();
falseを返します。
} ついに {
httpost.abort();
}
trueを返します。
}
private string getredirectlocation(){
bufferedheader locationheader =(bufferedheader)Response.getFirsTheader( "location");
if(locationheader == null){
nullを返します。
}
return locationheader.getValue();
}
プライベート文字列getText(string redirectlocation){
httpget httpget = new httpget(redirectlocation);
ResponseHandler <String> ResponseHandler = new Basic ResponseHandler();
文字列responsebody = "";
試す {
ResponseBody = httpclient.execute(httpget、responsehandler);
} catch(例外e){
e.printstacktrace();
ResponseBody = null;
} ついに {
httpget.abort();
//httpclient.getConnectionManager()。shutdown();
}
RESONSEBODY;
}
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();
}
}