Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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 webrequest?_C++_C_Json_Curl_Libcurl - Fatal编程技术网

C++ 如何生成libcurl HTTP webrequest?

C++ 如何生成libcurl HTTP webrequest?,c++,c,json,curl,libcurl,C++,C,Json,Curl,Libcurl,我正在尝试从url获取json数据: sub.domain.com/rest/getdiscounts?supermarketid=5323 我想获取从该url返回的数据。在终端,我可以这样做: curl --user user:password "http://sub.domain.com/rest/getdiscounts?supermarketid=5323" 到目前为止,我已经尝试了多种方法,例如: static string lastResponse; size_t server

我正在尝试从url获取json数据:

sub.domain.com/rest/getdiscounts?supermarketid=5323
我想获取从该url返回的数据。在终端,我可以这样做:

curl --user user:password "http://sub.domain.com/rest/getdiscounts?supermarketid=5323"
到目前为止,我已经尝试了多种方法,例如:

static string lastResponse;

size_t server_response_stream(void *ptr, size_t size, size_t nmemb, void *)
{
  lastResponse += (const char*)ptr;
  return size * nmemb;
}

bool get_request(string data1, string& errorDescription)
{
  CURL *curl = curl_easy_init();
  if (curl == NULL)
  {
    errorDescription = "Unable to initialise Curl";
    cout << errorDescription << std::endl;
    return false;
  }
  const string url = "http://sub.domain.com/rest/";
  curl_easy_setopt(curl, CURLOPT_URL, (url + "getdiscounts").c_str());
  curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_easy_setopt(curl, CURLOPT_USERPWD, USPA);

  char* dc1 = curl_easy_escape(curl, data1.c_str(), 0);
  string arg = "supermarketid=" + (string)dc1;
  const char* dt = arg.c_str();

  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dt);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(dt));

  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

  lastResponse = "";
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, server_response_stream);

  CURLcode res = curl_easy_perform(curl);

  res = curl_easy_perform(curl);

  if (res != CURLE_OK)
  {
    errorDescription = "Curl call to server failed!";
    cout << errorDescription << std::endl;
    return false;
  }

  curl_easy_cleanup(curl);
  return true;
}
静态字符串响应;
大小服务器响应流(void*ptr、大小、大小nmemb、void*)
{
lastResponse+=(常量字符*)ptr;
返回大小*nmemb;
}
bool get_请求(字符串数据1、字符串和错误描述)
{
CURL*CURL=CURL_easy_init();
if(curl==NULL)
{
errorDescription=“无法初始化卷曲”;

coutCURLOPT_WRITEFUNCTION函数的第一个参数
ptr
指向接收到的数据。该数据不是以null结尾的

字符串
+=
必须接收指向以null结尾的字符串的指针


调用
lastResponse+=(const char*)ptr;
正在导致未定义的行为。必须以不同的方式将字节追加到字符串中。您可以使用参数
size
nmemb
逐个字符追加字符串,以确定缓冲区的大小。

为什么要调用perform两次?@2501 First call验证第二次调用do t他工作。
curl——用户:密码”http://sub.domain.com/rest/getdiscounts?supermarketid=5323"
当您构造POST请求时,这看起来像GET请求,不是很奇怪吗?@Zero您发布的链接中的文章中也有相同的错误。@Zero在这种情况下,我认为没有必要调用perform两次。无论哪种方式,您都不会检查第一次调用的结果,这可能已经失败。为什么需要这样做?\@Zero我不确定您的意思那么。哪一部分不清楚?我在回答中解释了问题。我的意思是,当我直接设置lastResponse时,是什么导致了未定义的行为?@Zero行
lastResponse+=(const char*)ptr;
导致了未定义的行为,更具体地说是对重载运算符的底层调用。
static string responseStr;
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *)
{
  responseStr = (const char*)ptr;
  return size * nmemb;
}

string get_request()
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if (curl)
  {
    curl_easy_setopt(curl, CURLOPT_URL, "http://sub.domain.com/rest/getdiscounts?supermarketid=5323");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_easy_setopt(curl, CURLOPT_USERPWD, CREDENTIALS);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK)
    {
      cout << "curl_easy_perform() failed!" << std::endl;
      curl_easy_strerror(res);
      curl_easy_cleanup(curl);
      return "";
    }

    else if (res == CURLE_OK)
    {
      curl_easy_cleanup(curl);
      return responseStr;
    }
  }
}