【问题标题】:Getting verbose information from libcurl to a file从 libcurl 获取详细信息到文件
【发布时间】:2016-12-07 19:37:58
【问题描述】:

我正在尝试用 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 %d\n", res);
}
curl_easy_cleanup(curl);
fclose(filep);

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

【问题讨论】:

  • stderr 上是否显示正确的输出? (即控制台)
  • @johnelemans 详细信息不会在控制台上显示此代码。
  • 代码看起来没问题。确认没有重定向的情况下有输出到 stderr,如果没有,请先调试。
  • @johnelemans 如果我注释掉 curl_easy_setopt(curl, CURLOPT_STDERR, filep);它将详细信息打印到控制台。但是,我希望将这些信息保存在文件中。但是在这种情况下文件是空的。

标签: c++ qt curl libcurl


【解决方案1】:

以下示例适用于我:

#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;
}

【讨论】:

  • 你的例子对我不起作用。实际上,我复制了这个示例并使用了,但是日志文件仍然是空的。
  • 您使用的是什么操作系统、编译器和 curl 版本?我使用的是 Fedora 24、GCC 6.1 和 curl 7.47。
  • Windows 7 64 位,VS 2015 默认编译器和 curl 7.50.0
猜你喜欢
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 2013-05-29
  • 2022-08-19
  • 2012-03-22
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
相关资源
最近更新 更多