libcurl HTTPS POST data send?

前端 未结 2 1088
野性不改
野性不改 2020-12-30 08:02

I have application that received data via HTTP POST requests. I\'m trying to use libcurl to open a request to this app, send the data and receive the reply back from the app

相关标签:
2条回答
  • 2020-12-30 08:45

    I think you should also consider adding a handler for the response headers:

    cres = curl_easy_setopt( handle, CURLOPT_HEADERFUNCTION, HandleHeader );
    

    It may be that if this is a web service designed for use by an application, its reply is a header-only reply, with no response data. The header would contain data such as:

    HTTP/1.1 200 OK ...
    

    Where the main thing you're looking for is the 200 code saying the request was OK.

    The functions to handle the data from either header or response should take a void*, and you'll need to null-terminate it if you're using it as a string. For info this is how I handle the incoming data (in this case I only use the return code in my application - the response body just goes to a log file):

    static size_t CurlHandleResponse(
      void *Ptr,
      size_t Size,
      size_t NoElements,
      void* Data )
    {
      memcpy( &( gData[gDataLen] ), Ptr, (Size * NoElements) );
      gDataLen += (Size * NoElements);
      gData[ gDataLen ] = '\0';
      return (Size * NoElements);
    }
    
    ...
    ConvertUnprintable( gData, PrintStr );
    ...
    

    Error handling removed for brevity!

    0 讨论(0)
  • 2020-12-30 08:55

    You don't need to call any receive function. The received response comes back to you when control passes to the write-finished callback. Set up that callback something like this:

    curl_res = curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, RecvResponseCallback );

    before calling perform.

    Define the callback like this:

    size_t
    RecvResponseCallback ( char *ptr, size_t size, size_t nmemb, char *data ) {
      // handle received data
      return size * nmemb;
    }
    

    The response data is pointed by the data arg (not the ptr arg, which is something else that you will want to look at when you get this working).

    When you exit RecvResponseCallback, be sure to return (size_t) size * nmemb

    0 讨论(0)
提交回复
热议问题