Package com.lkb.test;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.client.ResponseHandler;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
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;
public class JD {
The Configuration Items
private static String RedirectURL = "Http://order.jd.com/center/list.action";
private static String loginurl = "Http://passport.jd.com/uc/login";
Don ' t change the following URL
private static String Renrenloginurl = "Https://passport.jd.com/uc/loginService";
The httpclient is used in one session
Private HttpResponse response;
Private Defaulthttpclient httpclient = new Defaulthttpclient ();
Public map<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 (key, value);
return map;
}
Private Boolean login () {
Map map = Getparams ();
HttpPost httpost = new HttpPost (Renrenloginurl);
All of the parameters post to the Web site
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 (New Basicnamevaluepair (key, value));
}
try {
Httpost.setentity (new urlencodedformentity (list<? extends org.apache.http.namevaluepair>) Nvps, http. Utf_8));
Response = Httpclient.execute (httpost);
catch (Exception e) {
E.printstacktrace ();
return false;
finally {
Httpost.abort ();
}
return true;
}
Private String getredirectlocation () {
Bufferedheader Locationheader = (bufferedheader) response.getfirstheader ("Location");
if (Locationheader = = null) {
return null;
}
return Locationheader.getvalue ();
}
private string GetText (string redirectlocation) {
HttpGet httpget = new HttpGet (redirectlocation);
responsehandler<string> ResponseHandler = new Basicresponsehandler ();
String responsebody = "";
try {
Responsebody = Httpclient.execute (HttpGet, ResponseHandler);
catch (Exception e) {
E.printstacktrace ();
Responsebody = null;
finally {
Httpget.abort ();
Httpclient.getconnectionmanager (). Shutdown ();
}
return 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 ();
}
}