有機肥

          綠色

          HttpClient4.4.1模擬登錄知乎(轉自http://blog.csdn.net/gaopu12345/article/details/45509387)

          一,登錄要Post的表單數據是什么

          這部分可以使用Wireshark工具來抓包就可以了,發現需要以下數據:

          “_xsrf” = xxxx(這是一個變動的數據,需要先活取獲取知乎首頁源碼來獲得) 
          “email” = 郵箱 
          “password” = 密碼 
          “rememberme” = “true” 
          “captcha” = 驗證碼(知乎有兩種驗證碼,你們可以去看,我使用的是數字字符的那種驗證碼)
          • 獲取_xsrf數據:
          String xsrfValue = responseHtml.split("<input type=\"hidden\" name= \"_xsrf\" value=\"")[1].split("\"/>")[0];
          responseHtml是首頁的源碼,根據網頁的組織形式,把_xsrf數據分割出來。

          RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();         
          CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
          HttpGet getHomePage = new HttpGet("http://www.zhihu.com/");
          try { //填充登陸請求中基本的參數
          CloseableHttpResponse response = httpClient.execute(getHomePage);
          String responseHtml = EntityUtils.toString(response.getEntity());
          String xsrfValue = responseHtml.split("<input type=\"hidden\" name=\"_xsrf\" value=\"")[1].split("\"/>")[0];
          System.out.println("_xsrf:" + xsrfValue);
          response.close();
          List<NameValuePair> valuePairs = new LinkedList<NameValuePair>();
          valuePairs.add(new BasicNameValuePair("_xsrf" , xsrfValue));
          valuePairs.add(new BasicNameValuePair("email", 用戶名));
          valuePairs.add(new BasicNameValuePair("password", 密碼));
          valuePairs.add(new BasicNameValuePair("rememberme", "true")); //獲取驗證碼
          HttpGet getCaptcha = new HttpGet("http://www.zhihu.com/captcha.gif?r=" + System.currentTimeMillis() + "&type=login");
          CloseableHttpResponse imageResponse = httpClient.execute(getCaptcha);
          FileOutputStream out = new FileOutputStream("/tmp/zhihu.gif");
          byte[] bytes = new byte[8192];
          int len;
          while ((len = imageResponse.getEntity().getContent().read(bytes)) != -1) {
          out.write(bytes,0,len); }
          out
          .close();
          Runtime.getRuntime().exec("eog /tmp/zhihu.gif");//ubuntu下看圖片的命令是eog //請用戶輸入驗證碼
          System.out.print("請輸入驗證碼:");
          Scanner scanner = new Scanner(System.in);
          String captcha = scanner.next();
          valuePairs.add(new BasicNameValuePair("captcha", captcha)); //完成登陸請求的構造
          UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8);
          HttpPost post = new HttpPost("http://www.zhihu.com/login/email");
          post.setEntity(entity); httpClient.execute(post);//登錄
          HttpGet g = new HttpGet("http://www.zhihu.com/question/following");//獲取“我關注的問題”頁面
          CloseableHttpResponse r = httpClient.execute(g);
          System.out.println(EntityUtils.toString(r.getEntity()));
          r.close(); } catch (IOException e) {
          e.printStackTrace(); } finally {
          try { httpClient.close(); }
          catch (IOException e) {
          e.printStackTrace();
          }
          }
          此處要注意開頭的RequestConfig,我一開始是沒有設置cookie這方面的額內容的,結果一直提示有cookie錯誤,所以查看了HttpClient手冊,上面提到了選擇Cookie策略,通過這種方法設置一個全局的Cookie策略,
          RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();//標準Cookie策略
          CloseableHttpClient httpClient = HttpClients
          .custom().setDefaultRequestConfig(requestConfig).build();//設置進去

          posted on 2017-07-10 17:50 有機肥 閱讀(139) 評論(0)  編輯  收藏

          <2017年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 海安县| 清丰县| 沙湾县| 延边| 克什克腾旗| 三门峡市| 芷江| 长丰县| 隆回县| 通渭县| 彰化县| 阿荣旗| 深泽县| 安新县| 牡丹江市| 揭阳市| 德令哈市| 白银市| 于田县| 眉山市| 上栗县| 盘锦市| 贡山| 长顺县| 金华市| 赞皇县| 鹤庆县| 丹江口市| 林西县| 铜梁县| 福海县| 鸡西市| 周至县| 会昌县| 武清区| 东阳市| 大足县| 双柏县| 滦南县| 虞城县| 宁城县|