复制代码代码如下:
包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();
}
}