模拟淘宝登陆成功了,但跳转实现不了!急--100分!

xyz 2013-08-27 01:30:46
我用HttpURLConnection实现了淘宝登陆,返回的信息也是我登陆成功,里面也包含了,跳转的连接,但是接下来我或许cookies并和我要请求的页面一起上传,但是登陆信息还是登陆之前的信息,不过只是显示不用登陆了而已!


URL url = new URL("https://login.taobao.com/member/login.jhtml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8");
connection.setRequestProperty("Content-Length", String.valueOf(paramStr.length()));
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 7.0; Windows 7)");
connection.connect();

OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream(), "utf-8");
out.write("TPL_username=淘宝帐号&TPL_password=淘宝密码&action=Authenticator&event_submit_do_login=anything"); // post的关键所在!
out.flush();
out.close();
String cookieVal = null;
String key = null;
String cookies = "";
for (int i = 1; (key = connection.getHeaderField(i)) != null; i++) {
if (key.equalsIgnoreCase("Set-Cookie")) {
cookieVal = connection.getHeaderField(i);
cookieVal = cookieVal.substring(0,
cookieVal.indexOf(";"));
cookies = cookies + cookieVal + ";";
}
}
String sessionId = null;
if(cookieVal != null)
{
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
}

int res = connection.getResponseCode();
if (res == 200)
{
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "GBK"));
String retVal;
while ((retVal = in.readLine()) != null)
{
System.out.println(retVal);
}
}
//下面的连接是我需要登陆之后请求的连接!
URL url1=new URL("http://trade.taobao.com/trade/itemlist/listBoughtItems.htm?spm=a1z09.2.7.3.DKKSn9&action=itemlist/QueryAction&event_submit_do_query=1&auctionStatus=ALL&commentStatus=I_HAS_NOT_COMMENT");
HttpURLConnection con=(HttpURLConnection) url1.openConnection();
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Cookie",cookieVal);
con.setUseCaches(false);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8");
con.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream(), "GBK"));
String retVal;
while ((retVal = in.readLine()) != null)
{
System.out.println(retVal);
}

}

不知道哪里错了,希望感兴趣的朋友一起研究!!!!!!!
以下是我获取token的过程!!!
String token=null;
Document doc = Jsoup.connect("http://trade.taobao.com/trade/trade/itemlist/list_bought_items.htm?spm=a1z02.1.0.d1000304.GYRAvv&event_submit_do_query=1&action=itemlist%2FQueryAction&user_type=0&_fmt.q._0.c=I_HAS_NOT_COMMENT&_fmt.q._0.au=ALL&tracelog=mytaobao_daipingjia").get();
Elements _tb_token=doc.select("html>body>img");
for (Element ele:_tb_token) {
String tokenstring = ele.attr("src");
if(tokenstring.length()==78){
token = tokenstring.substring(45);
System.out.print(token);

}
}
//通过解析获得了token,准备下次请求的时候加上去!
URL url1=new URL("http://trade.taobao.com/trade/itemlist/listBoughtItems.htm?spm=a1z09.2.7.3.DKKSn9&action=itemlist/QueryAction&event_submit_do_query=1&auctionStatus=ALL&commentStatus=I_HAS_NOT_COMMENT");

HttpURLConnection con=(HttpURLConnection) url1.openConnection();
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Cookie",token);
...全文
1162 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
oh_Maxy 2013-09-01
  • 打赏
  • 举报
回复
引用 6 楼 xyz 的回复:
[quote=引用 5 楼 oh_Maxy 的回复:] 你的token只是为了标注已登录的这个状态是吧? 看最后一样的命名,似乎是想放到cookie里是吧?放到request里,会导致页面关掉后,这个属性就没了的。
对得token就是登陆的令牌!之后请求其他页面只需要token加上用户民就可以了![/quote] 嗯,那token要放到cookie作用域,然后每次进入一个页面前,拦截器首先从cookie里取token,如果合法,就允许继续访问就好啦。
xyz 2013-09-01
  • 打赏
  • 举报
回复
引用 5 楼 oh_Maxy 的回复:
你的token只是为了标注已登录的这个状态是吧? 看最后一样的命名,似乎是想放到cookie里是吧?放到request里,会导致页面关掉后,这个属性就没了的。
对得token就是登陆的令牌!之后请求其他页面只需要token加上用户民就可以了!
oh_Maxy 2013-09-01
  • 打赏
  • 举报
回复
你的token只是为了标注已登录的这个状态是吧? 看最后一样的命名,似乎是想放到cookie里是吧?放到request里,会导致页面关掉后,这个属性就没了的。
2222ddddddd 2013-09-01
  • 打赏
  • 举报
回复
哥们登陆问题解决了吗
xyz 2013-08-27
  • 打赏
  • 举报
回复
为什么加上了token还是请求不了其他页面呢?
xyz 2013-08-27
  • 打赏
  • 举报
回复
原来是需要登录认证之后才会办法token给自己!也就是传说中的令牌,之后访问,就只需要NICK加上token就可以了!

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧