How to get the HTTP response string using Curl in C++

后端 未结 2 1743
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 22:48

I\'m very new to HTTP commands and the libcurl library. I know how to get the HTTP response code but not the HTTP response string. Following is the code snippet that I wrote to

2条回答
  •  温柔的废话
    2021-02-09 23:42

    For the numerical response code, getinfo with CURLINFO_RESPONSE_CODE is the way to go:

    long response_code;
    curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, long &response_code);
    

    However there is no equivalent getinfo capture for the server's response text. If you need the server's text, inspect the raw HTTP headers. There are two ways to do this:

    1. Enable writing headers to the payload with CURLOPT_HEADER, then extract the headers from the combined payload, splitting the body on \n\n

    2. Set a header callback function with CURLOPT_HEADERFUNCTION and parse directly from that

提交回复
热议问题