从libcurl获取详细信息到文件

Getting verbose information from libcurl to a file

本文关键字:文件 详细信息 获取 libcurl      更新时间:2023-10-16

我正试图在C/C++中开发一个使用libcurl的QT应用程序。简单地说,我想将VERBOSE数据保存到一个文件中。在libcurl API文档中,据说(https://curl.haxx.se/libcurl/c/CURLOPT_VERBOSE.html)

详细信息将发送到stderr,或用CURLOPT_stderr设置的流。

因此,VERBOSE信息将在stderr中。在我关注CURLOPT_STDERR的链接之后(https://curl.haxx.se/libcurl/c/CURLOPT_STDERR.html)告诉,

将FILE*作为参数传递。告诉libcurl在显示进度表和显示CURLOPT_VERBOSE数据时使用此流而不是stderr。

在CURLOPT_STDERR链接中,存在一个代码示例。我在自己的应用程序上尝试过:

CURL *curl = curl_easy_init();
FILE *filep = fopen("dump.txt", "wb");
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
    curl_easy_setopt(curl, CURLOPT_STDERR, filep);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_perform(curl);
}
CURLcode res = curl_easy_perform(curl);

if (CURLE_OK != res) {      
    fprintf(stderr, "curl told us %dn", res);
}
curl_easy_cleanup(curl);
fclose(filep);

但是,详细信息不会显示在命令行中,并且为详细信息创建的文件是空的。我该如何解决这个问题?

以下示例适用于我:

#include <stdio.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;
  FILE* logfile;
  logfile = fopen("dump.txt", "wb");
  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_URL, "http://example.org");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  curl_easy_setopt(hnd, CURLOPT_STDERR, logfile);
  ret = curl_easy_perform(hnd);
  curl_easy_cleanup(hnd);
  fclose(logfile);
  return (int)ret;
}