手机
当前位置:查字典教程网 >编程开发 >Java >java实现京东登陆示例分享
java实现京东登陆示例分享
摘要:复制代码代码如下:packagecom.lkb.test;importjava.util.ArrayList;importjava.util...

复制代码 代码如下:

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

}

}

【java实现京东登陆示例分享】相关文章:

java hashtable实现代码

java实现大文件分割与合并的实例代码

java字符串反转示例分享

java 实现约瑟夫环的实例代码

java使用数组和链表实现队列示例

java与js代码互调示例代码

在java中使用dom解析xml的示例分析

java解析xml之sax解析xml示例分享

java单向链表的实现实例

java裁剪图片并保存的示例分享

精品推荐
分类导航