httpclient自动登录淘宝网,并处理相关网页

文章分类:Java编程

svn下载源码(使用svn工具下载):

svn://www.svnhost.cn/gaoqs_open_source/taobao_login/trunk/com.gaoqs.auto.login

(相关的jar包,请看文档,就3个,大家自己找一下,或是下载svn里有)

Java代码 
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";   
            //下面2个值从httpwatch中得到   
            String taobaoPwd="3DES_2_000000000000000000000000000000_61F0B8BE021BBBDD020919017B6816F5";   
            String taobaoTid="XOR_1_000000000000000000000000000000_63584054400B0F717B750370";   
               
            HttpClient client = new HttpClient();   
            client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);   
               
            //取得_tb_token_值   
            String _tb_token_Value="";   
            Cookie[] cookies = client.getState().getCookies();           
            String responseString = processGet(client,null,TAOBAO_BASE_LOGIN_BEFORE,cookies,true,true);      
  
            //取第二个_tb_token_为,现在的登录方式   
            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);   
                   
            //post请求,发送登录验证   
            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();   
               
            //处理302内部跳转   
            responseString = processGet(client, null, redirectUrl, cookies, true, true);   
               
            //打开我的票页面   
            responseString=responseString.substring(0,responseString.indexOf("我的彩票"));   
            System.out.println("main(String[]) - 我的彩票页面返回内容为:" + responseString);    
            //解析返回的html字符串,得到,彩票购买的链接   
            String caiPiaoUrl=responseString.substring(responseString.lastIndexOf("<a")+"<a href='".length(),responseString.lastIndexOf(">")-1);   
            //用get方式,模拟请求,最后一个参数为false,表示,我不需要返回值,我只要访问该网址就可以   
            //这是为了   
            processGet(client, null, caiPiaoUrl, cookies, true, false);   
               
            //下面还等什么?实际登录你的淘宝号,找到相应的链接,用httpwatch检查参数   
            //模拟提交吧。   
               
               
               
        }   
           
           
        /**  
         * get方式请求  
         *   
         * @param client httpclient  
         * @param get getmethond  
         * @param url 地址  
         * @param cookies 请求时附加 cookies  
         * @param needAppendCookies 是否返回后附加 cookies  
         * @param needResponse 是否需要返回  
         * @return  
         * @throws IOException  
         */  
        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 "";   
        }   
           
        /**  
         * post方式请求  
         *   
         * @param client httpclient  
         * @param post  postmethond  
         * @param url 地址  
         * @param params post参数  
         * @param cookies 请求时附加cookies  
         * @param needAppendCookies 是否在返回后附加cookies  
         * @param needResponse 是否需要返回内容  
         * @return  
         * @throws IOException  
         */  
        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 "";   
        }   
           
        /**  
         * 截取字符串    
         * etc.   processDetail("<a href='g.cn'</td>","href='","</td","href='".length(),"</td".length()-1),返回g.cn  
         * @param tempStr 原字串   
         * @param startFlag 开始标识  
         * @param endFlag 结束标识  
         * @param starts 开始+  
         * @param ends 结束-  
         * @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;   
        }   
  
}