複製代碼代碼如下:
包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.defaulthtpclient;
導入org.apache.http.message.basicnamevaluepair;
導入org.apache.http.message.bufferedheader;
導入org.apache.http.protocol.http;
導入com.util.constant;
公共類JD {
//配置項目
私有靜態字符串redirecturl =“ http://order.jd.com/center/list.action”;
私有靜態字符串loginurl =“ http://passport.jd.com/uc/login”;
//不要更改以下URL
私有靜態字符串renrenloginurl =“ https://passport.jd.com/uc/loginservice”;
// httpclient用於一個會話
私人httpresponse響應;
私有defaulthtpclient httpclient = new defaulthtpclient();
公共地圖<字符串,字符串> getParams(){
MAP <String,String> map = new Hashmap <String,String>();
字符串str = getText(loginurl);
字符串strs1 [] = str.split(“ name =/” uuid/“ value =/”);
字符串strs2 [] = strs1 [1] .split(“/“/>”);
字符串uuid = strs2 [0];
map.put(“ uuid”,uuid);
system.out.println(strs2 [0]);
字符串str3s [] = strs1 [1] .split(“ <span class =/“ clr/”> </span> <input type =/“ hidden/“ nidend/” name =/“”);
字符串strs4 [] = str3s [1] .split(“/>”);
字符串strs5 [] = strs4 [0] .trim()。split(“/“”);
字符串鍵= strs5 [0];
字符串值= strs5 [2];
map.put(鍵,值);
返回地圖;
}
私人布爾登錄(){
地圖地圖= getParams();
httppost httpost =新的httppost(renrenloginurl);
//所有參數發佈到網站
列表<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));
迭代器it = map.keyset()。iterator();
while(it.hasnext()){
字符串鍵= it.next()。toString();
字符串值= map.get(key).toString();
nvps.add(new BasicNameValuePair(key,value));
}
嘗試 {
httpost.setentity(new urlencodedformentity((列表<?擴展org.apache.http.namevaluepair>)NVPS,http.utf_8));
響應= httpclient.execute(httpost);
} catch(異常E){
e.printstacktrace();
返回false;
} 最後 {
httpost.abort();
}
返回true;
}
私有字符串getRedirectLocation(){
bufferedheader locationheader =(bufferedheader)響應。getFirStheader(“位置”);
if(locationheader == null){
返回null;
}
返回locationheader.getValue();
}
私有字符串getText(字符串redirectlocation){
httpget httpget = new httpget(redirectlocation);
Response Handler <string> responseHandler = new BasicResponseHandler();
字符串響應body =“”;
嘗試 {
responsebody = httpclient.execute(httpget,wendesphandler);
} catch(異常E){
e.printstacktrace();
響應body = null;
} 最後 {
httpget.abort();
//httpclient.getConnectionManager()。shutdown();
}
返迴響應機體;
}
public void printText(){
如果(login()){
system.out.println(getText(redirecturl));
字符串redirectLocation = getRedirectLocation();
if(redirectlocation!= null){
system.out.println(getText(redirectLocation));
}
}
}
公共靜態void main(string [] args){
JD enren = new JD();
// renren.getParams();
enren.printtext();
}
}