①用Fiddler2追蹤登錄時的post請求,發現需要以下參數:

          check

          uname

          backURL

          autoLogin

          pwd

          其中,backURL="/",check=“1”,autoLogin可默認為1

          于是,只剩 uname和 pwd

          ②創建一個HttpClient

          private DefaultHttpClient httpclient = new DefaultHttpClient();

          ③創建一個 HttpPost

          HttpPost httpost = new HttpPost(CommonConst.loginUrl);

          ④偽裝httpost,騙過服務器

              /**
               * pretend to be a browser quietly
               
          */
              
          private void setPostHeader(HttpPost post) {
                  post.setHeader(CommonConst.UserAgent, CommonConst.HttpAgent);
                  post.setHeader(
          "Origin", CommonConst.weiboUrl);
                  post.setHeader(
          "Cache-Control""max-age=0");
                  post.setHeader(
          "Accept",
                          
          "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                  post.setHeader(
          "Accept-Encoding""gzip,deflate,sdch");
                  post.setHeader(
          "Accept-Language""en-US,en;q=0.8");
                  post.setHeader(
          "Accept-Charset""ISO-8859-1,utf-8;q=0.7,*;q=0.3");
                  post.setHeader(
          "Accept-Encoding""gzip,deflate,sdch");
                  post.setHeader(
          "Referer", CommonConst.loginUrl);
              }

           ⑤創建NameValuePair

          List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
                  nvps.add(
          new BasicNameValuePair("check"this.check));
                  nvps.add(
          new BasicNameValuePair("uname"this.uname));
                  nvps.add(
          new BasicNameValuePair("backURL"this.backURL));
                  nvps.add(
          new BasicNameValuePair("autoLogin"this.autoLogin));
                  nvps.add(
          new BasicNameValuePair("pwd"this.pwd));

          ⑥用setEntity方法,給httpost設置相關參數

          httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

          ⑦向相應的host上提交post請求

          HttpHost targetHost = new HttpHost(CommonConst.host);
                      response 
          = httpclient.execute(targetHost, httpost);


          login代碼:

          private boolean login() {
                  
          // httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
                  HttpPost httpost = new HttpPost(CommonConst.loginUrl);
                  setPostHeader(httpost);
                  
          // All the parameters post to the web site
                  List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
                  nvps.add(
          new BasicNameValuePair("check"this.check));
                  nvps.add(
          new BasicNameValuePair("uname"this.uname));
                  nvps.add(
          new BasicNameValuePair("backURL"this.backURL));
                  nvps.add(
          new BasicNameValuePair("autoLogin"this.autoLogin));
                  nvps.add(
          new BasicNameValuePair("pwd"this.pwd));

                  
          try {
                      httpost.setEntity(
          new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                      HttpHost targetHost 
          = new HttpHost(CommonConst.host);
                      response 
          = httpclient.execute(targetHost, httpost);
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                      
          return false;
                  } 
          finally {
                      httpost.abort();
                  }
                  
          return true;
              }


          附CommonConst.java

          package com.yinger;

          public class CommonConst {
              
          public static String HttpAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5";
              
          public static String loginUrl = "http://m.weibo.cn/login";
              
          public static String host = "m.weibo.cn";
              
          public static String weiboUrl = "http://m.weibo.cn";
              
          public static String UserAgent = "User-Agent";
          }



          posted on 2012-07-11 11:59 Ying-er 閱讀(5248) 評論(5)  編輯  收藏

          評論:
          # re: 用httpclient模擬瀏覽器,登錄新浪微博 2012-09-11 14:36 |
          能否問下,用Fiddler2 如何追蹤到這些參數的。請教一下  回復  更多評論
            
          # re: 用httpclient模擬瀏覽器,登錄新浪微博[未登錄] 2012-11-24 00:17 | tang
          what library should we included in it ?  回復  更多評論
            
          # re: 用httpclient模擬瀏覽器,登錄新浪微博 2013-01-06 14:51 | auto714
          HttpClient 開源包@tang
            回復  更多評論
            
          # re: 用httpclient模擬瀏覽器,登錄新浪微博 2013-01-06 14:52 | auto714
          親!有嘗試過發送微博么?  回復  更多評論
            
          # re: 用httpclient模擬瀏覽器,登錄新浪微博 2013-01-09 11:04 | restart
          扯呢么,Fiddler2 得到的參數,遠不止這些。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 鹤岗市| 咸宁市| 牙克石市| 巨鹿县| 蒙山县| 栖霞市| 宕昌县| 云南省| 邢台县| 陆川县| 绵阳市| 轮台县| 双城市| 安徽省| 威海市| 渭源县| 赫章县| 资溪县| 铜山县| 阿城市| 南开区| 望谟县| 沅江市| 长乐市| 化德县| 南川市| 容城县| 石首市| 台北县| 丽江市| 瓦房店市| 望都县| 宝清县| 东兴市| 康保县| 娄烦县| 淮北市| 瑞昌市| 乐昌市| 象州县| 万盛区|