今天调试了一天了,怎么都登陆不上,下面是我的代码,各位大虾看看。
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Log {
private static final String LOGON_SITE = "http://www.taobao.com";
private static final int PORT = 80;
private static final String loginurl = "http://login.taobao.com/member/login.jhtml";
private static final String tokenurl = "http://login.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F";
public static void main(String args[]) throws HttpException, IOException {
//创建连接
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, PORT);
Cookie[] cookies = client.getState().getCookies();
//获取_tb_token
GetMethod get = new GetMethod(tokenurl);
if(cookies!=null)
get.setRequestHeader("Cookie" , cookies.toString());
client.executeMethod(get);
cookies = client.getState().getCookies(); //获取返回的cookie信息
client.getState().addCookies(cookies);
System.out.println(cookies.toString());
String response=get.getResponseBodyAsString().toString();
//System.out.println(response);
response.substring(response.indexOf("_tb_token_")+"_tb_token_".length());
response = response.substring(response.indexOf("_tb_token_")+"_tb_token_".length());
String value=response.substring(response.indexOf("value=")+"value='".length(),response.indexOf(">")-1);
get.releaseConnection();
System.out.println("tbtoken = "+value);
//登陆..
String user="xxxxxxx";
PostMethod post = new PostMethod(loginurl);
NameValuePair[] params= new NameValuePair[] {
new NameValuePair("_tb_token",value),
new NameValuePair("action", "Authenticator"),
new NameValuePair("callback",""),
new NameValuePair("css_style",""),
new NameValuePair("CtrlVersion","1,0,0,7"),
new NameValuePair("event_submit_do_login", "anything"),
new NameValuePair("fc","2"),
new NameValuePair("from","tb"),
new NameValuePair("from_encoding",""),
new NameValuePair("guf",""),
new NameValuePair("gvfdcname","10"),
new NameValuePair("isIgnore",""),
new NameValuePair("llnick",""),
new NameValuePair("loginType", "4"),
new NameValuePair("longLogin","-1"),
new NameValuePair("minipara",""),
new NameValuePair("minititle",""),
new NameValuePair("need_sign",""),
new NameValuePair("need_user_id",""),
new NameValuePair("not_duplite_str",""),
new NameValuePair("popid",""),
new NameValuePair("poy",""),
new NameValuePair("pstrong","2"),
new NameValuePair("sign",""),
new NameValuePair("style","default"),
new NameValuePair("support","000001"),
new NameValuePair("tid","XOR_1_000000000000000000000000000000_63584425437F79740F06767C"),
new NameValuePair("TPL_password", "3DES_2_000000000000000000000000000000_FE39B07DFF60A631"),
new NameValuePair("TPL_redirect_url", "http://www.taobao.com/"),
new NameValuePair("TPL_username", user),
};
post.setRequestBody(params);
post.setRequestHeader("Cookie" , cookies.toString());
client.executeMethod(post);
cookies = client.getState().getCookies(); //获取返回的cookie信息
client.getState().addCookies(cookies); //打印服务器返回信息
String postresponse = post.getResponseBodyAsString().toString();
System.out.println("post="+postresponse);
post.releaseConnection();
}
}
从服务器返回的信息是空,也就是postresponse是空。搞了一天了头都大了,是我登陆表单填写错了吗,还是什么问题啊..
另外还有就是提交登陆表单的时候,那个tid是哪里来的呢? 虽然我是利用httpwatch抓取到,但是始终不明白是什么作用。
谢谢大虾们的帮忙啊~
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Log {
private static final String LOGON_SITE = "http://www.taobao.com";
private static final int PORT = 80;
private static final String loginurl = "http://login.taobao.com/member/login.jhtml";
private static final String tokenurl = "http://login.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F";
public static void main(String args[]) throws HttpException, IOException {
//创建连接
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, PORT);
Cookie[] cookies = client.getState().getCookies();
//获取_tb_token
GetMethod get = new GetMethod(tokenurl);
if(cookies!=null)
get.setRequestHeader("Cookie" , cookies.toString());
client.executeMethod(get);
cookies = client.getState().getCookies(); //获取返回的cookie信息
client.getState().addCookies(cookies);
System.out.println(cookies.toString());
String response=get.getResponseBodyAsString().toString();
//System.out.println(response);
response.substring(response.indexOf("_tb_token_")+"_tb_token_".length());
response = response.substring(response.indexOf("_tb_token_")+"_tb_token_".length());
String value=response.substring(response.indexOf("value=")+"value='".length(),response.indexOf(">")-1);
get.releaseConnection();
System.out.println("tbtoken = "+value);
//登陆..
String user="xxxxxxx";
PostMethod post = new PostMethod(loginurl);
NameValuePair[] params= new NameValuePair[] {
new NameValuePair("_tb_token",value),
new NameValuePair("action", "Authenticator"),
new NameValuePair("callback",""),
new NameValuePair("css_style",""),
new NameValuePair("CtrlVersion","1,0,0,7"),
new NameValuePair("event_submit_do_login", "anything"),
new NameValuePair("fc","2"),
new NameValuePair("from","tb"),
new NameValuePair("from_encoding",""),
new NameValuePair("guf",""),
new NameValuePair("gvfdcname","10"),
new NameValuePair("isIgnore",""),
new NameValuePair("llnick",""),
new NameValuePair("loginType", "4"),
new NameValuePair("longLogin","-1"),
new NameValuePair("minipara",""),
new NameValuePair("minititle",""),
new NameValuePair("need_sign",""),
new NameValuePair("need_user_id",""),
new NameValuePair("not_duplite_str",""),
new NameValuePair("popid",""),
new NameValuePair("poy",""),
new NameValuePair("pstrong","2"),
new NameValuePair("sign",""),
new NameValuePair("style","default"),
new NameValuePair("support","000001"),
new NameValuePair("tid","XOR_1_000000000000000000000000000000_63584425437F79740F06767C"),
new NameValuePair("TPL_password", "3DES_2_000000000000000000000000000000_FE39B07DFF60A631"),
new NameValuePair("TPL_redirect_url", "http://www.taobao.com/"),
new NameValuePair("TPL_username", user),
};
post.setRequestBody(params);
post.setRequestHeader("Cookie" , cookies.toString());
client.executeMethod(post);
cookies = client.getState().getCookies(); //获取返回的cookie信息
client.getState().addCookies(cookies); //打印服务器返回信息
String postresponse = post.getResponseBodyAsString().toString();
System.out.println("post="+postresponse);
post.releaseConnection();
}
}
从服务器返回的信息是空,也就是postresponse是空。搞了一天了头都大了,是我登陆表单填写错了吗,还是什么问题啊..
另外还有就是提交登陆表单的时候,那个tid是哪里来的呢? 虽然我是利用httpwatch抓取到,但是始终不明白是什么作用。
谢谢大虾们的帮忙啊~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货