Java implementation of the Beijing-East landing sample sharing _java

Source: Internet
Author: User
Tags constant gettext uuid

Copy Code code as follows:

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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.