国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HttpClient4.4.1模擬登錄知乎

2019-11-15 00:03:54
字體:
來源:轉載
供稿:網友
HttpClient4.4.1模擬登錄知乎HttpClient4.4.1模擬登錄知乎一,登錄要Post的表單數據是什么

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

"_xsrf" = xxxx(這是一個變動的數據,需要先活取獲取知乎首頁源碼來獲得)"email" = 郵箱"passWord" = 密碼"rememberme" = "y"(或者n也可以)

  • 獲取_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 get = new HttpGet("http://www.zhihu.com/");        try {            CloseableHttPResponse response = httpClient.execute(get);            String responseHtml = EntityUtils.toString(response.getEntity());            String xsrfValue = responseHtml.split("<input type=/"hidden/" name=/"_xsrf/" value=/"")[1].split("/"/>")[0];            System.out.println("xsrfValue:" + 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", "y"));            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8);            HttpPost post = new HttpPost("http://www.zhihu.com/login");            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();//設置進去

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 托克托县| 资兴市| 聂拉木县| 文成县| 武山县| 彭阳县| 都兰县| 云龙县| 青川县| 特克斯县| 东港市| 池州市| 永登县| 吴川市| 长丰县| 祥云县| 通榆县| 县级市| 越西县| 宜川县| 乌兰察布市| 泗阳县| 特克斯县| 抚宁县| 昌邑市| 哈巴河县| 阳东县| 米易县| 岳普湖县| 瑞丽市| 乌拉特中旗| 江阴市| 诸城市| 蓬安县| 韩城市| 靖宇县| 海城市| 鸡东县| 南涧| 康定县| 北辰区|