1

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

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

运行我的应用程序时,我成功登录到该页面,当我随后尝试获取另一个页面时,(页面)数据表明我没有登录 - 即“状态”不知何故丢失了。

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

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

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

这是我的课程中处理 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);

上面的代码有什么问题吗?

4

2 回答 2

2

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

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

示例代码:

#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;
}
于 2010-02-23T14:31:24.753 回答