Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ libcurl HTTP请求将响应保存到变量-c++;_C++_Httprequest_Libcurl - Fatal编程技术网

C++ libcurl HTTP请求将响应保存到变量-c++;

C++ libcurl HTTP请求将响应保存到变量-c++;,c++,httprequest,libcurl,C++,Httprequest,Libcurl,我试图将HTTP请求返回的数据保存到一个变量中 下面的代码将自动打印请求的响应,但我需要它将响应保存为字符或字符串 int main(void) { char * result; CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://ww

我试图将HTTP请求返回的数据保存到一个变量中

下面的代码将自动打印请求的响应,但我需要它将响应保存为字符或字符串

int main(void)
{
        char * result;
        CURL *curl;
        CURLcode res;
        curl = curl_easy_init();
        if(curl) {
            curl_easy_setopt(curl, CURLOPT_URL, "http://www.browsarity.com/");

            res = curl_easy_perform(curl);
            curl_easy_cleanup(curl);
        }
        return 0;
 }

我认为您必须编写一个函数,通过
CURLOPT_WRITEFUNCTION
作为写回调传递(请参阅)。或者,您可以创建一个临时文件,并通过
CURLOPT_WRITEDATA
传递其文件描述符(该页面上列出的下一个选项)。然后将临时文件中的数据读回字符串。不是最漂亮的解决方案,但至少你不必弄乱缓冲区和函数指针

编辑:由于您不想写入文件,类似这样的操作可能会起作用:

#include <string>

size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
  ((string*)stream)->append((char*)ptr, 0, size*count);
  return size*count;
}

int main(void) {
  // ...
  if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.browsarity.com/");

    string response;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    // The "response" variable should now contain the contents of the HTTP response
  }
  return 0;
}
#包括
大小写入字符串(void*ptr、大小、大小计数、void*stream){
((字符串*)流)->追加((字符*)ptr,0,大小*计数);
返回大小*计数;
}
内部主(空){
// ...
if(curl){
curl\u easy\u setopt(curl,CURLOPT\u URL,“http://www.browsarity.com/");
字符串响应;
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,write\u to\u string);
curl_easy_setopt(curl、CURLOPT_WRITEDATA和response);
res=旋度(curl)\u容易执行(curl);
旋度\轻松\清洁(旋度);
//“response”变量现在应该包含HTTP响应的内容
}
返回0;
}

免责声明:我没有测试过,我对C++有点生疏,但是你可以试试。

这里是你的一个例子。
T.Yates是对的,您必须创建一个函数来接收数据。并使用CURLOPT_WRITEFUNCTION让CURL了解您的函数

为了让代码更容易理解,我将使用类似这样的
write\u To\u string
函数

size_t write_to_string(void *ptr, size_t size, size_t nmemb, std::string stream)
{
  size_t realsize = size * nmemb;
  std::string temp(static_cast<const char*>(ptr), realsize);
  stream.append(temp);
  return realsize;
}
size\u t write\u to\u string(void*ptr、size\u t size、size\u t nmemb、std::string stream)
{
大小\u t realsize=大小*nmemb;
std::字符串温度(静态转换(ptr)、realsize);
附加流(temp);
返回realsize;
}

我已经在那个链接中了,但我不知道如何使用指针和所有的东西来实现它。文件这件事对我不好,因为我有少量的数据,我需要它非常快。对我来说很有效。我使用string*作为write_to_string中的最后一个参数类型,这节省了我的铸造时间。您能告诉我如何进行更多的细节吗?比如在哪里放置CURLOPT_WRITEFUNCTION?在这里,您的
std::string流是函数的本地流,因此函数结束后所有数据都将丢失。。。