欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现京东登陆示例分享

程序员文章站 2024-02-23 11:30:04
复制代码 代码如下:package com.lkb.test; import java.util.arraylist;import java.util.hashmap;i...

复制代码 代码如下:

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