1. 程式人生 > >使用cookie繞過驗證碼進行模擬登入

使用cookie繞過驗證碼進行模擬登入

1.工具
httpClient jar包
任意抓包工具 推薦 firefox的 firebug(非常好用)

在使用httpClient進行模擬登入時,有時候會遇到需要輸入驗證碼的情況。而對驗證碼進行識別的方法既費時效果還不好。這裡提供一個取巧的方法,即使用瀏覽器登入時產生的cookie進行登入。

2.上程式碼
首先是一個模擬http請求的工具類

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity
; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache
.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class Http { static CloseableHttpClient httpclient = null;
static RequestConfig requestConfig = null; static { requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build(); httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build(); } public static String doGet(String url) { RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build(); try { // 建立httpget. HttpGet httpget = new HttpGet(url); httpget.addHeader("Cookie", "xxxxxxx"); httpget.addHeader("Accept-Encoding","gzip, deflate"); httpget.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); httpget.addHeader("Cache-Control","no-cache"); httpget.addHeader("Connection","keep-alive"); httpget.addHeader("Content-Type","application/json; charset=UTF-8"); httpget.addHeader("Referer","http://www.zhihu.com/"); httpget.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"); System.out.println("executing request " + httpget.getURI()); // 執行get請求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 獲取響應實體 HttpEntity entity = response.getEntity(); System.out.println("--------------------------------------"); // 列印響應狀態 System.out.println(response.getStatusLine()); if (entity != null) { return EntityUtils.toString(entity); } System.out.println("------------------------------------"); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /**以utf-8形式讀取*/ public static String doPost(String url,List<NameValuePair> formparams) { return doPost(url, formparams,"UTF-8"); } public static String doPost(String url,List<NameValuePair> formparams,String charset) { RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build(); // 建立預設的httpClient例項. // CloseableHttpClient httpclient = HttpClients.createDefault(); // 建立httppost HttpPost httppost = new HttpPost(url); httppost.addHeader("Cookie", "xxxxxxx"); httppost.addHeader("Accept-Encoding","gzip, deflate"); httppost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); httppost.addHeader("Cache-Control","no-cache"); httppost.addHeader("Connection","keep-alive"); httppost.addHeader("Content-Type","application/json; charset=UTF-8"); httppost.addHeader("Referer","http://www.zhihu.com/"); httppost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, charset); httppost.setEntity(uefEntity); CloseableHttpResponse response = httpclient.execute(httppost); try { // 列印響應狀態 System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }

httppost.addHeader(“Cookie”, “xxxxxxx”); 這裡xxxxxx的即為登入時產生的cookie 由於每個瀏覽器產生的cookie不盡相同,故以xxxxx代替

測試類 這裡模擬登入的是知乎

public class Test {


    public static void main(String[] args) {
        String content = Http.doGet("http://www.zhihu.com/");
        System.out.println(content);
    }

}

測試結果
這裡寫圖片描述