如何在C ++中使用Curl获取HTTP响应字符串

时间:2017-07-09 07:46:41

标签: c++ string http curl response

我对HTTP命令和libcurl库都很陌生。我知道如何获取HTTP响应代码而不是HTTP响应字符串。以下是我为获取响应代码而编写的代码片段。任何有关如何获取响应字符串的帮助都将受到高度赞赏!!!

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;
}

我也试过这样做以获取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";

但是readBuffer是空的。我不明白我哪里出错了。关于如何解决这个问题的任何指示都非常好!

2 个答案:

答案 0 :(得分:6)

您的代码看起来并没有太大的错误。我根据你的代码运行了以下代码,阅读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;
}

...我得到了完整的HTML回复。 NB我必须使用代理,并且我启用了详细模式以查看发生了什么。也NB; HTTP服务器可能对您提供的URL很挑剔:如果我用http://www.bbc.co.uk/news替换http://www.bbc.co.uk/news/(即使用尾部斜杠),那么我没有得到任何数据(响应长度为零),如上所述通过详细的卷曲输出:

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

这里,Content-Length为0,从不调用WriteCallback()函数。希望这可以帮助。

答案 1 :(得分:0)

对于数字响应代码,可以使用getinfo with CURLINFO_RESPONSE_CODE

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

但是,服务器的响应文本没有等效的getinfo捕获。如果需要服务器的文本,请检查原始HTTP标头。有两种方法可以做到这一点:

  1. 启用写入headers to the payload with CURLOPT_HEADER,然后从组合的有效负载中提取标头,在\n\n上拆分正文

  2. 设置一个header callback function with CURLOPT_HEADERFUNCTION并直接从中进行解析