卷曲度(u easy)(u perform)&(x27);不要等待在URL上提供JSON输入

卷曲度(u easy)(u perform)&(x27);不要等待在URL上提供JSON输入,json,http,libcurl,Json,Http,Libcurl,我已经编写了以下代码 CURL *curl_handle; struct curl_slist *headers=NULL; int ret; curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_appe

我已经编写了以下代码

CURL *curl_handle;
  struct curl_slist *headers=NULL;
  int ret;

  curl_global_init(CURL_GLOBAL_ALL);

  curl_handle = curl_easy_init();
  headers = curl_slist_append(headers, "Content-Type: application/json");
  headers = curl_slist_append(headers, "Accept: application/json");
  curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://<url>");
  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
  curl_easy_setopt(curl_handle, CURLOPT_HTTPGET,1);
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer);

  int httpCode(0);
  std::unique_ptr<std::string> httpData(new std::string());
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, httpData.get());
  ret = curl_easy_perform(curl_handle);
CURL*CURL\u手柄;
struct curl_slist*headers=NULL;
int ret;
curl\u global\u init(curl\u global\u ALL);
curl_handle=curl_easy_init();
headers=curl\u slist\u append(headers,“内容类型:application/json”);
headers=curl\u slist\u append(headers,“Accept:application/json”);
curl_easy_setopt(curl_句柄、CURLOPT_HTTPHEADER、headers);
curl\u easy\u setopt(curl\u句柄,CURLOPT\u URL,“http://”);
卷曲容易设置(卷曲手柄,卷曲程序,1L);
卷曲容易设置(卷曲手柄,卷曲详细,1L);
curl_easy_setopt(curl_句柄,CURLOPT_HTTPGET,1);
curl\u easy\u setopt(curl\u句柄、CURLOPT\u WRITEFUNCTION、writer);
int-httpCode(0);
std::unique_ptr httpData(新std::string());
curl_easy_setopt(curl_句柄、CURLOPT_WRITEDATA、httpData.get());
ret=卷曲轻松执行(卷曲手柄);
根据我的理解,curl_easy_perform应该等待获取json作为它的资源。但不幸的是,我的程序没有等待JSON资源就返回了

我希望libcurl等待forjson资源,一旦它可用,那么只有它才能继续处理获得的数据

我是否遗漏了代码中的某些内容,或者我的理解不正确?
请帮助我进一步了解此问题。

您的程序发出HTTP GET请求,然后(可能?)返回响应。libcurl向您提供服务器响应。它不知道要等待的任何特定资源(JSON或其他)。我们真的不清楚你想要你的程序做什么而它还没有做…行
curl\u easy\u setopt(curl\u handle,CURLOPT\u WRITEFUNCTION,writer)
将名为
writer
的函数设置为从服务器获取响应时的回调函数。这只会在通过
curl\u easy\u perform(curl\u handle)发送请求后发生。如果JSON资源是您期望服务器在请求后返回的响应,那么在调用它时,您可以在
writer
函数中获得它。