从libcurl获取详细信息到文件

时间:2016-08-02 12:36:06

标签: c++ qt curl libcurl

我正在尝试使用libcurl在C / C ++中开发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 *作为参数。在显示进度表并显示CURLOPT_VERBOSE数据时,告诉libcurl使用此流而不是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 %d\n", res);
}
curl_easy_cleanup(curl);
fclose(filep);

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

1 个答案:

答案 0 :(得分:0)

以下示例适用于我:

#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;
}
相关问题