Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Problem trying to use Curl in C++ to get http response string after sending http string to server.

From: José Fernández via curl-library <curl-library_at_cool.haxx.se>
Date: Mon, 12 Oct 2020 10:45:27 +0000 (UTC)

 Thanks,

I have gotten it to work. The code that I currently use in the Curl client is this:
 
 
#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());
    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;
    else
      std::cout << "Error sening message." << 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 << "The answer from server is: " << 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 valueVar here.
    // ...
    valueVar = 5;
 
    std::string msgToSend("CONTITION-IS-TRUE");
    if (valueVar == 5)
      sendDataAndGetAnswer(msgToSend);
     
    sleep(10);
  }
 
  return 0;
}



    En viernes, 25 de septiembre de 2020 19:53:08 CEST, Dan Fandrich via curl-library <curl-library_at_cool.haxx.se> escribió:
 
 On Fri, Sep 25, 2020 at 04:01:08PM +0000, José Fernández via curl-library wrote:
> When the program tries to get the response from the server sends it always
> shows on the screen:
>    curl_easy_perform() failed to get answer: Timeout was reached
> I have tried to increase the timeout and I get the same error.

It sounds like perhaps the server isn't sending a properly-formatted reply so
libcurl times out waiting for it. Try using the debug callbacks or Wireshark to
see what the server is returning.

Also, there's something odd about the program. It's structured to perform two
independent transfers, but the first specifies no destination for the received
data (so it will go to stdout) while the second specifies no source for the
data to send (so it will repeat the POST). I'm guessing that perhaps you really
wanted was to perform a single transfer rather than two, which would involve
moving the CURLOPT_WRITE* options into the first transfer.

Dan
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette:  https://curl.haxx.se/mail/etiquette.html

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2020-10-12