转载: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会请求失败