国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現(xiàn)京東登陸示例分享

2019-11-26 15:38:33
字體:
供稿:網(wǎng)友

復制代碼 代碼如下:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 锦屏县| 开封市| 海林市| 宝应县| 锡林浩特市| 新田县| 龙南县| 芒康县| 昌宁县| 海兴县| 牙克石市| 工布江达县| 新干县| 如皋市| 多伦县| 屏南县| 革吉县| 田东县| 永仁县| 兴山县| 霍州市| 共和县| 浦北县| 岱山县| 南澳县| 黄大仙区| 广东省| 始兴县| 增城市| 马龙县| 乌拉特后旗| 肇东市| 邛崃市| 合江县| 漳平市| 鄂尔多斯市| 彭阳县| 崇明县| 麻城市| 全州县| 大邑县|