[英]How to get the HTTP response string using Curl in C++
I'm very new to HTTP commands and the libcurl library.我对 HTTP 命令和 libcurl 库很陌生。 I know how to get the HTTP response code but not the HTTP response string.
我知道如何获取 HTTP 响应代码但不知道 HTTP 响应字符串。 Following is the code snippet that I wrote to get the response code.
以下是我为获取响应代码而编写的代码片段。 Any help on how to get the response string will be highly appreciated!!!
任何有关如何获取响应字符串的帮助将不胜感激!!!
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
CURLcode ret = curl_easy_perform(curl);
if (ret != CURLE_OK) {
LOG(INFO) << "Failed to perform the request. "
<< "Return code: " << ret;
return false;
}
std::unique_ptr<int64_t> httpCode(new int64_t);
// Get the last response code.
ret = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, httpCode.get());
if (ret != CURLE_OK) {
LOG(INFO) << "curl_easy_getinfo failed to retrieve http code. "
<< "Return code: " << ret;
return false;
}
I tried doing this as well to get the HTTP response string in readBuffer.我也尝试这样做以在 readBuffer 中获取 HTTP 响应字符串。
static size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
CURLcode ret = curl_easy_perform(curl);
cout << readBuffer << "\n";
But the readBuffer is empty.但是 readBuffer 是空的。 I don't understand where I am going wrong.
我不明白我哪里出错了。 Any pointers on how to solve this will be really nice!
关于如何解决这个问题的任何指示都非常好!
There doesn't look to be much wrong with your code.您的代码看起来没有太大问题。 I ran the following code, based on yours, to read the front page of the BBC news website:
我根据您的代码运行了以下代码,以阅读 BBC 新闻网站的首页:
#include <iostream>
#include <string>
#include <curl/curl.h>
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(int argc, char * argv[])
{
curl_global_init(CURL_GLOBAL_ALL);
CURL* easyhandle = curl_easy_init();
std::string readBuffer;
curl_easy_setopt(easyhandle, CURLOPT_URL, "http://www.bbc.co.uk/news");
curl_easy_setopt(easyhandle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(easyhandle, CURLOPT_PROXY, "http://my.proxy.net"); // replace with your actual proxy
curl_easy_setopt(easyhandle, CURLOPT_PROXYPORT, 8080L);
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_perform(easyhandle);
std::cout << readBuffer << std::endl;
return 0;
}
... and I got the full HTML response. ...我得到了完整的 HTML 响应。 NB I had to use a proxy, and I enabled verbose mode to see what was happening.
注意我必须使用代理,并且我启用了详细模式以查看发生了什么。 Also NB;
也请注意; the HTTP server might be fussy about the URL you give it: if I replace http://www.bbc.co.uk/news with http://www.bbc.co.uk/news/ (ie with a trailing slash) then I get no data back (a response length of zero), as noted by the verbose curl output:
HTTP 服务器可能对您提供的 URL 很挑剔:如果我将http://www.bbc.co.uk/news替换为http://www.bbc.co.uk/news/ (即用尾部斜杠) 然后我没有返回任何数据(响应长度为零),如详细的 curl 输出所示:
Host: www.bbc.co.uk
Accept: */*
Proxy-Connection: Keep-Alive
< HTTP/1.1 301 Moved Permanently
< X-Cache-Action: PASS (no-cache-control)
< Vary: Accept-Encoding
< X-Cache-Age: 0
< Content-Type: text/html;charset=utf-8
< Date: Mon, 10 Jul 2017 16:42:20 GMT
< Location: http://www.bbc.co.uk/news
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
< X-Cache: MISS from barracuda1.[my proxy].net
< Connection: keep-alive
<
* Connection #0 to host [my proxy] left intact
Here, Content-Length is 0 and the WriteCallback() function is never called.此处,Content-Length 为 0,并且永远不会调用 WriteCallback() 函数。 Hope this helps.
希望这可以帮助。
For the numerical response code, getinfo with CURLINFO_RESPONSE_CODE
is the way to go:对于数字响应代码,使用
CURLINFO_RESPONSE_CODE
是可行的方法:
long response_code;
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE,&response_code);
However there is no equivalent getinfo capture for the server's response text.但是,服务器的响应文本没有等效的 getinfo 捕获。 If you need the server's text, inspect the raw HTTP headers.
如果您需要服务器的文本,请检查原始 HTTP 标头。 There are two ways to do this:
有两种方法可以做到这一点:
Enable writing headers to the payload with CURLOPT_HEADER
, then extract the headers from the combined payload, splitting the body on \n\n
使用
CURLOPT_HEADER
启用将标头写入有效载荷,然后从组合的有效载荷中提取标头,将正文拆分为\n\n
Set a header callback function with CURLOPT_HEADERFUNCTION
and parse directly from that使用
CURLOPT_HEADERFUNCTION
设置标头回调函数并直接从中解析
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.