无法存储libcurl结果。 (输出空文件或不写入内存)

时间:2019-09-18 09:11:41

标签: c libcurl

我正在尝试建立一个可以在游戏中击败您的connect4,并且由于我不是AI方面的专家,所以我决定从网站上获取计算机的操作(我得到了JSON文件)。 我尝试了许多解决方案,但libcurl看起来效果最好。我设法在提示符下获得正确的输出。

这很奇怪,因为当我不想将其存储在内存中并且一旦我尝试保存它时,我就会看到它的显示消失。我尝试过其他网站,但只有我想要的网站受到影响。

这是JSON想要的http://connect4.gamesolver.org/solve?pos=44

但是当我试图将字符串放入内存或文件中时,它什么也没写。 (我使用了libcurl提供的代码...

https://curl.haxx.se/libcurl/c/url2file.html我只是得到一个空文件

https://curl.haxx.se/libcurl/c/getinmemory.html我说的是0个字节)。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <curl/curl.h>

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int main(int argc, char *argv[])
{
  CURL *curl_handle;
  static const char *pagefilename = "page.out";
  FILE *pagefile;

  if(argc < 2) {
    printf("Usage: %s <URL>\n", argv[0]);
    return 1;
  }

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */ 
  curl_handle = curl_easy_init();

  /* set URL to get here */ 
  curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);

  /* Switch on full protocol/debug output while testing */ 
  curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

  /* disable progress meter, set to 0L to enable and disable debug output */ 
  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

  /* send all data to this function  */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

  /* open the file */ 
  pagefile = fopen(pagefilename, "wb");
  if(pagefile) {

    /* write the page body to this file handle */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

    /* get it! */ 
    curl_easy_perform(curl_handle);

    /* close the header file */ 
    fclose(pagefile);
  }

  /* cleanup curl stuff */ 
  curl_easy_cleanup(curl_handle);

  curl_global_cleanup();

  return 0;
}

1 个答案:

答案 0 :(得分:1)

您没有设置CURLOPT_WRITEFUNCTION(libcurl为实际写入数据而应调用的函数)和CURLOPT_WRITEDATA(应传递给该函数的流)。再次查看您的first tutorial

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int main(int argc, char *argv[])
{
    // ...
    /* send all data to this function  */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /* open the file */ 
    pagefile = fopen(pagefilename, "wb");
    if(pagefile) {

      /* write the page body to this file handle */ 
      curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

      /* get it! */ 
      curl_easy_perform(curl_handle);

      /* close the header file */ 
      fclose(pagefile);
    }

    // ...
}

结果是您的请求实际上已发送,收到了答复,但数据未写入任何地方。

更新

更新后的帖子中的代码对我来说很好。因此,我认为您的问题出在其他地方。试试这个代替原来的文件处理部分:

  char errorbuf[ CURL_ERROR_SIZE ] = "";
  curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorbuf); 

  /* open the file */ 
  pagefile = fopen(pagefilename, "wb");
  if(pagefile) {

    /* write the page body to this file handle */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

    /* get it! */ 
    if ( curl_easy_perform(curl_handle) != CURLE_OK )
    {
      fputs( errorbuf, stderr );
    }

    /* close the header file */ 
    fclose(pagefile);
  }
  else
  {
    perror( "fopen failed" );
  }

请注意,尽管教程通常会跳过适当的错误处理,但我在上面展示的内容还是应该成为“真实”代码的一部分。