C++ 如何在C++;将http字符串发送到服务器后获取http响应字符串

C++ 如何在C++;将http字符串发送到服务器后获取http响应字符串,c++,curl,libcurl,C++,Curl,Libcurl,在C++程序中,我需要使用Curl向另一台运行用C++编写的REST服务器的机器发送http消息,然后从该服务器收集响应 我对使用Curl还不熟悉,并且一直在查看web上的文档和各种示例 我的程序的源代码如下所附 将消息发送到服务器可以工作,但我没有收到服务器发送给我的响应(不是超时问题) 我将非常感谢您的帮助,因为我不确定用于从服务器获取响应的代码是否正确 #include <stdio.h> #include <iostream> #include <unis

在C++程序中,我需要使用Curl向另一台运行用C++编写的REST服务器的机器发送http消息,然后从该服务器收集响应

我对使用Curl还不熟悉,并且一直在查看web上的文档和各种示例

我的程序的源代码如下所附

将消息发送到服务器可以工作,但我没有收到服务器发送给我的响应(不是超时问题)

我将非常感谢您的帮助,因为我不确定用于从服务器获取响应的代码是否正确


#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <curl/curl.h>



size_t getAnswerFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
    data->append((char*)ptr, size * nmemb);
    return size * nmemb;
}



void sendDataAndGetAnswer(std::string data)
{
  CURL *curl;
  CURLcode res;
  struct curl_slist *httpHeaders=NULL;

  curl = curl_easy_init();
  if (curl)
  {
    curl_global_init(CURL_GLOBAL_ALL);
    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
    
    httpHeaders = curl_slist_append(httpHeaders, "MyProgram-Header");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpHeaders);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "MyAgent/1.0");
    curl_easy_setopt(curl, CURLOPT_POST, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000);

    // .........................................
    // MyProgram sends data.
    // This works right.
    // .........................................

    res = curl_easy_perform(curl);
    if (res != CURLE_OK)
      std::cout << "curl_easy_perform() failed to send message: " << curl_easy_strerror(res) << std::endl;


    // .........................................
    // MyProgram get answer.
    // This does not work.
    // .........................................

    std::string response_string;
    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getAnswerFunction);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
    // getAnswerFunction waits at most 5000 ms.
    // Afterwards, the flow of the program should continue even if there is no response.
    curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000);

    res = curl_easy_perform(curl);
    if (res == CURLE_OK)
      std::cout << response_string;
    else
      std::cout << "curl_easy_perform() failed to get answer: " << curl_easy_strerror(res) << std::endl;


    curl_easy_cleanup(curl);
    curl_slist_free_all(httpHeaders);
  }
}



int main(void)
{

  while (1)
  {
    int valueVar = 0;
    // Execute various statements and perform calculations.
    // ...

    // Calculate value here.
    // ...

    std::string msgToSend("CONTITION-IS-TRUE");
    if (valueVar = 5)
      sendDataAndGetAnswer(msgToSend);
    
    sleep(10);
  }


  return 0;
}


#包括
#包括
#包括
#包括
#包括
size\u t getAnswerFunction(void*ptr、size\u t size、size\u t nmemb、std::string*data){
数据->附加((字符*)ptr,大小*nmemb);
返回大小*nmemb;
}
void sendDataAndGetAnswer(标准::字符串数据)
{
卷曲*卷曲;
卷曲编码;
struct curl_slist*httpHeaders=NULL;
curl=curl_easy_init();
if(curl)
{
curl\u global\u init(curl\u global\u ALL);
curl\u easy\u setopt(curl,CURLOPT\u URL,“http://192.168.0.100:15000");
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,data.c_str());
httpHeaders=curl\u slist\u append(httpHeaders,“MyProgram Header”);
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,httpHeaders);
curl_easy_setopt(curl,CURLOPT_USERAGENT,“MyAgent/1.0”);
卷曲容易设置(卷曲,卷曲杆,1L);
curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,1000);
// .........................................
//我的程序发送数据。
//这是正确的。
// .........................................
res=旋度(curl)\u容易执行(curl);
如果(res!=卷曲(OK)

你能说“它不工作”吗?但它怎么“不工作”呢?发生了什么?应该发生什么?预期的输出是什么?实际的输出是什么?请花一些时间刷新,并详细说明您的问题。此外,您是否尝试过使用调试器检查发生了什么?例如,在
getAnswerFunction
中放置断点以查看它得到了什么参数?以及什么时候创建一个,请尝试确保它不包含任何不相关的错误。最好创建一个复制问题(仅)的精确最小示例,并按原样复制粘贴到问题中。我之所以提到这一点,是因为在代码中显示的if(valueVar=5)
似乎不正确。当程序运行时“RES = CURLNASEYYAX执行(CURL);”为了从服务器获得答案,RES的值不是CURLYOK。虽然超时非常大,但我得到超时错误。考虑启用选项来查看内部LIGBURL正在做什么,这可能会给您一个线索。