1. 程式人生 > >libcurl 設定代理,通過Fiddler可以進行抓包

libcurl 設定代理,通過Fiddler可以進行抓包

轉載:https://blog.csdn.net/jaryguo/article/details/53021923

用libcurl在專案開發過程中,除錯階段需要進行抓包測試,但Fiddler不能收到應用的Http連線。

Google了一下,因為應用用了libcurl的介面來建立HTTP連線,如果要使用Fiddler,需要在程式碼中插入類似如下的程式碼:

curl_easy_setopt(m_curl, CURLOPT_PROXY, "127.0.0.1:8888");

其中8888是Fiddler預設設定的一個監聽埠,如果在Option中修改了,則需要替換為響應的埠號。

void Test()
{
    CURL
* curl; char *url = "http://develop.test.com"; std::stringstream out; curl = curl_easy_init(); //設定url curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_PROXY, "127.0.0.1:8888"); curl_easy_setopt(curl, CURLOPT_POST, 0);//設定為非0表示本次操作為POST // 設定接收資料的處理函式和存放變數
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);//設定回撥函式 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); // 執行HTTP GET操作 CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); }
// 接受資料存放在out中,輸出之 //cout << out.str() << endl; string str_json = out.str(); printf("%s", str_json.c_str()); curl_easy_cleanup(curl); }

注:如果程式碼中加入了設定代理,只有在抓包工具執行的情況下,http才能請求成功,進而能抓到資料;否則http會請求失敗