`
eimhee
  • 浏览: 2112535 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

httpclient3 自动登陆淘宝, 开心网

    博客分类:
  • JAVA
阅读更多


前提:
需要用到的java包
commons-httpclient-3.1.jar
commons-logging.jar
log4j-1.2.15.jar
commons-codec.jar

登陆淘宝的例子:

package com.spider.cron;


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.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


public class TaoBaoLogin {

		private static final String LOGON_SITE = "http://www.taobao.com";
		private static final int LOGON_PORT = 80;
	
		private static final String TAOBAO_BASE_LOGIN_BEFORE = "http://member1.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F";
		private static final String TAOBAO_BASE_LOGIN = "http://login.taobao.com/member/login.jhtml";

		public static void main(String args[]) throws HttpException, IOException {
			String taobaoUser="woshigoojje@163.com";
			
			String taobaoPwd="3DES_2_000000000000000000000000000000_61F0B8BE021BBBDD020919017B6816F5";
			String taobaoTid="XOR_1_000000000000000000000000000000_63584054400B0F717B750370";
			
			HttpClient client = new HttpClient();
			client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
			
		
			String _tb_token_Value="";
			Cookie[] cookies = client.getState().getCookies();        
	        String responseString = processGet(client,null,TAOBAO_BASE_LOGIN_BEFORE,cookies,true,true);   

	     
	        responseString=responseString.substring(responseString.indexOf("_tb_token_")+"_tb_token_".length());
	        responseString=responseString.substring(responseString.indexOf("_tb_token_")+"_tb_token_".length());
	        _tb_token_Value=responseString.substring(responseString.indexOf("value=")+"value='".length(),responseString.indexOf(">")-1);
	       
	        PostMethod post = new PostMethod(TAOBAO_BASE_LOGIN);
	        NameValuePair[] params= new NameValuePair[] {        		
	        new NameValuePair("_oooo_", ""),
			new NameValuePair("_tb_token_", _tb_token_Value),
			new NameValuePair("abtest", ""),
			new NameValuePair("action", "Authenticator"),
			new NameValuePair("actionForStable", "enable_post_user_action"),
			new NameValuePair("CtrlVersion", "1,0,0,7"),
			new NameValuePair("done", ""),
			new NameValuePair("event_submit_do_login", "anything"),
			new NameValuePair("from", ""),
			new NameValuePair("loginType", "4"),
			new NameValuePair("mcheck", ""),
			new NameValuePair("mi_uid", ""),
			new NameValuePair("pstrong", ""),
			new NameValuePair("support", "000001"),
			new NameValuePair("tid", taobaoTid),
			new NameValuePair("TPL_password", taobaoPwd),
			new NameValuePair("TPL_redirect_url", ""),
			new NameValuePair("TPL_redirect_url", ""),
			new NameValuePair("TPL_redirect_url", ""),
			new NameValuePair("TPL_redirect_url", ""),
			new NameValuePair("TPL_redirect_url", ""),
			new NameValuePair("TPL_redirect_url", ""),
			new NameValuePair("TPL_username", taobaoUser),
			new NameValuePair("yparam", "")
	        };
	        processPost(client, post, TAOBAO_BASE_LOGIN, params, cookies, true, false);
			Header header=post.getResponseHeader("Location");
			String redirectUrl=header.getValue();
			
			
			responseString = processGet(client, null, redirectUrl, cookies, true, true);
			
			
			responseString=responseString.substring(0,responseString.indexOf("我的彩票"));
			System.out.println("main(String[]) - " + responseString); 

			String caiPiaoUrl=responseString.substring(responseString.lastIndexOf("<a")+"<a href='".length(),responseString.lastIndexOf(">")-1);
	
			processGet(client, null, caiPiaoUrl, cookies, true, false);

			
			
		}
		
		

		public static String processGet(HttpClient client,GetMethod get,String url,Cookie[] cookies,boolean needAppendCookies,boolean needResponse) throws IOException{
			if(client==null || url==null || url=="") return "";
			if(get==null)
				get=new GetMethod();
			get = new  GetMethod(url);  
			if(cookies!=null)
				get.setRequestHeader("Cookie" , cookies.toString());
	        client.executeMethod(get);
	        if(needAppendCookies){
		        cookies = client.getState().getCookies();   
		        client.getState().addCookies(cookies);   
	        }
	        if(needResponse)
	        	return get.getResponseBodyAsString();   
			get.releaseConnection();
			return "";
		}
		

		public static String processPost(HttpClient client,PostMethod post,String url,NameValuePair[] params,Cookie[] cookies,boolean needAppendCookies,boolean needResponse) throws IOException{
			if(client==null || url==null || url=="") return "";
			if(post==null)
				post = new PostMethod(url);
			
			if(params!=null && params.length>0)
				post.setRequestBody(params);
			if(cookies!=null)
				post.setRequestHeader("Cookie" , cookies.toString());
			client.executeMethod(post);
			if(needAppendCookies){
		        cookies = client.getState().getCookies();   
		        client.getState().addCookies(cookies);   
	        }
			if(needResponse)
	        	return post.getResponseBodyAsString();
			post.releaseConnection();
			return "";
		}
		

		public static String processDetail(String tempStr,String startFlag,String endFlag,int starts,int ends){
			if(tempStr==null || "".equals(tempStr)) return "";
			int start=tempStr.indexOf(startFlag);
			int end=tempStr.indexOf(endFlag);
			if(start==-1 || end==-1 || (end-ends)<(start+starts)) return "";
			try{
			tempStr=tempStr.substring(start+starts,end-ends);
			}catch(Exception e){
				System.out.println("processDetail(String, String, String, int, int) " + e.toString()); 
				return "";
			}
			return tempStr;
		}

}

 

 

 

登陆开心网的例子:

 

package com.spider.cron;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class LoginKaixin {
    private static final String LOGON_SITE = "http://www.kaixin001.com";

    private static final int LOGON_PORT = 80;

    public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);

        // 登录页面
        PostMethod post = new PostMethod(
                "http://www.kaixin001.com/login/login.php");
        NameValuePair ie = new NameValuePair("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
        NameValuePair url = new NameValuePair("url", "/home/");
        NameValuePair username = new NameValuePair("email", "xxx@163.com");
        NameValuePair password = new NameValuePair("password", "xxxxxx");
        post.setRequestBody(new NameValuePair[] { ie, url, username, password });
        client.executeMethod(post);
        System.out
                .println("******************************登录******************************");
        Cookie[] cookies = client.getState().getCookies();
        client.getState().addCookies(cookies);
        post.releaseConnection();
        System.out
                .println("******************************页面转向******************************");
        String newUrl = "http://www.kaixin001.com/home/";
        System.out.println("==========Cookies============");
        int i = 0;
        for (Cookie c : cookies) {
            System.out.println(++i + ":   " + c);
        }
        client.getState().addCookies(cookies);
        post.releaseConnection();
        GetMethod get = new GetMethod(newUrl);
        get.setRequestHeader("Cookie", cookies.toString());
        client.executeMethod(get);
        String responseString = get.getResponseBodyAsString();
        // 登录后首页的内容
        System.out.println(responseString);
        get.releaseConnection();
        System.out
                .println("******************************组件功能******************************");
        // "http://www.kaixin001.com/!slave/index.php", "朋友买卖"
        // "http://www.kaixin001.com/!parking/index.php", "争车位"
        // "http://www.kaixin001.com/!house/index.php?_lgmode=pri", "买房子"
        // http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49
        // "http://www.kaixin001.com/!house/garden/index.php","花园"
        // (1)进入朋友买卖****************
        System.out
                .println("******************************(1)进入朋友买卖******************************");
        String slave = "http://www.kaixin001.com/!slave/index.php";
        get = new GetMethod(slave);
        get.setRequestHeader("Cookie", cookies.toString());
        client.executeMethod(get);
        responseString = get.getResponseBodyAsString();
        System.out.println(responseString);
        get.releaseConnection();
        // (2)进入争车位****************
        System.out
                .println("******************************(2)进入争车位******************************");
        String parking = "http://www.kaixin001.com/!parking/index.php";
        get = new GetMethod(parking);
        get.setRequestHeader("Cookie", cookies.toString());
        client.executeMethod(get);
        responseString = get.getResponseBodyAsString();
        System.out.println(responseString);
        get.releaseConnection();
        // (3)进入买房子****************
        System.out
                .println("******************************(3)进入买房子*******************************");
        String house = "http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49";
        get = new GetMethod(house);
        get.setRequestHeader("Cookie", cookies.toString());
        client.executeMethod(get);
        responseString = get.getResponseBodyAsString();
        System.out.println(responseString);
        get.releaseConnection();
        // (4)进入花园****************
        System.out
                .println("******************************(4)进入花园*******************************");
        String garden = "http://www.kaixin001.com/!house/garden/index.php";
        get = new GetMethod(garden);
        get.setRequestHeader("Cookie", cookies.toString());
        client.executeMethod(get);
        responseString = get.getResponseBodyAsString();
        System.out.println(responseString);
        get.releaseConnection();

    }

}

 

12
2
分享到:
评论
12 楼 dingxanghua 2013-07-12  
淘宝登录还是用c#的webbroser控件吧,就6行左右代码,httpclient的这个玩不起来
11 楼 dingxanghua 2013-07-12  
对我的帮助很大,对httpclient认识更深刻了。
10 楼 zhutianxiang 2013-04-13  
你这段登录淘宝的目前已经无效了。是解析登录页面,获取"_tb_token_"时出错了,登录页面现在搜索不到这个字符串了。能修改一下再提供吗?可以加我扣,我有后续问题可能需要你帮助,非常感谢。16530202
9 楼 ctfzh 2010-11-18  
不错,学习了
8 楼 long502147 2010-11-15  
加个验证码!这个就不灵了~
7 楼 zrf8579 2010-11-14  
我测试了一下,如果登录时勾选“使用安全控件”,password处就是使用3DES方式,如果不勾选,直接会以明文方式发送TPL_password
使用安全控件的loginType为4,不使用的话loginType为3
6 楼 eimhee 2010-11-14  
zrf8579 写道
taobaoPwd和taobaoTid两个值怎么获得的?可以生成么?

要抓包工具才能获取到
5 楼 zrf8579 2010-11-13  
taobaoPwd和taobaoTid两个值怎么获得的?可以生成么?
4 楼 ustcfrank 2010-11-11  
yangguo 写道
有什么用呢?

秒杀
3 楼 yuhe 2010-11-10  
my god
xie xie lou zhu  这样的话都不能发。
2 楼 eimhee 2010-11-10  
yangguo 写道
有什么用呢?

写外挂, 自动发帖, 自动投票的第一步就是要先登陆
1 楼 yangguo 2010-11-10  
有什么用呢?

相关推荐

Global site tag (gtag.js) - Google Analytics