簡體   English   中英

C ++卷曲后動態var

[英]C++ Curl post dynamic var

我想在POST curl中使用動態變量
我使用以下代碼:

int send(const char*s)
{
  CURL *curl;
  CURLcode res;


  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/query.php");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "q=" + s);
    res = curl_easy_perform(curl);

    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  std::cout << std::endl << "Query sent" << std::endl;
  return 0;
}

我得到這個錯誤:

test.cpp:199:57: error: invalid operands of types ‘const char [3]’ and ‘const char*’ to binary ‘operator+’
         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "q=" + s);
                                                    ~~~~~^~~

您必須自己連接"q="s ,Cpp中沒有運算符+可以將chars數組與指向chars的指針連接起來。 使用"q="創建字符串,將s指向的數據添加到此字符串,然后調用c_str()以獲取const char*指針作為curl_easy_setopt函數的參數:

#include <string>
....
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/query.php");
std::string buf("q=");
buf += s;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf.c_str());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM