如何(即使用什么工具)来监控Curl发送的标头(Cookie问题)

时间:2010-02-23 12:45:19

标签: c++ cookies curl libcurl

我在C ++应用程序中使用Curl(libcurl),我无法发送cookie(我认为)。

我安装了Fiddler,TamperData和LiveHTTP Headers,但它们仅用于查看浏览器流量,并且(似乎)无法监控计算机上的一般网络流量,所以当我运行我的机器时,我看不到要发送的标头信息。但是,当我在浏览器中查看页面时,如果成功登录,我可以看到正在发送cookie信息。

当我运行我的应用程序时,我成功登录页面,当我随后尝试获取另一页时,(页面)数据表明我没有登录 - 即“状态”以某种方式丢失。

我的C ++代码看起来没问题,所以我不知道出了什么问题 - 这就是我需要的原因:

  1. 首先能够查看我的机器网络流量(而不仅仅是浏览器流量) - 哪个(免费)工具?

  2. 假设我错误地使用了Curl,我的代码有什么问题? (正在检索和存储cookie,似乎它们由于某种原因而没有被请求发送。

  3. 以下是我班级中处理Http请求的cookie方面的部分:

    curl_easy_setopt(curl, CURLOPT_TIMEOUT, long(m_timeout));
    curl_easy_setopt(curl, CURLOPT_USERAGENT,
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; WOW64; SV1; .NET CLR 2.0.50727)");
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
    

    以上代码有什么问题吗?

2 个答案:

答案 0 :(得分:2)

您可以使用Wireshark(以前的Ethereal)查看计算机发送和接收的所有网络流量。

答案 1 :(得分:0)

  1. 正如Sean Carpenter所说,Wireshark是查看网络流量的正确工具。开始捕获并使用http作为过滤器,仅查看HTTP流量。如果您只想查看Curl发送/接收的HTTP请求/响应,请设置CURL_VERBOSE选项并查看stderr:curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L)
  2. 我相信你正确使用Curl。编译并运行以下(完整)示例;你会看到第二次你运行它(当cookies.txt存在时)将cookie发送到服务器。
  3. 示例代码:

    #include <stdio.h>
    #include <curl/curl.h>
    
    int main()
    {
        CURL *curl;
        CURLcode success;
        char errbuf[CURL_ERROR_SIZE];
        int m_timeout = 15;
    
        if ((curl = curl_easy_init()) == NULL) {
            perror("curl_easy_init");
            return 1;
        }
    
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, long(m_timeout));
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
        curl_easy_setopt(curl, CURLOPT_USERAGENT,
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; WOW64; SV1; .NET CLR 2.0.50727)");
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt");
        curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
    
        if ((success = curl_easy_perform(curl)) != 0) {
            fprintf(stderr, "%s: %s\n", "curl_easy_perform", errbuf);
            return 1;
        }
    
        curl_easy_cleanup(curl);
        return 0;
    }
    
相关问题