有什么方法可以简单地用c++发出HTTP请求吗?具体来说,我想下载一个页面(一个API)的内容,并检查内容,看看它是否包含1或0。是否也可以将内容下载到字符串中?


当前回答

HTTP协议非常简单,因此编写HTTP客户端非常简单。 这里有一个

https://github.com/pedro-vicente/lib_netsockets

它使用HTTP GET从web服务器检索文件,服务器和文件都是命令行参数。远程文件保存为本地副本。

声明:我是作者

检查http.cc https://github.com/pedro-vicente/lib_netsockets/blob/master/src/http.cc

int http_client_t::get(const char *path_remote_file)
{
  char buf_request[1024];

  //construct request message using class input parameters
  sprintf(buf_request, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", path_remote_file, m_server_ip.c_str());

  //send request, using built in tcp_client_t socket
  if (this->write_all(buf_request, (int)strlen(buf_request)) < 0)
  {
    return -1;
  }

编辑:编辑URL

其他回答

现在正在开发一种更新的、不太成熟的卷曲包装器,称为c++ Requests。下面是一个简单的GET请求:

#include <iostream>
#include <cpr.h>

int main(int argc, char** argv) {
    auto response = cpr::Get(cpr::Url{"http://httpbin.org/get"});
    std::cout << response.text << std::endl;
}

它支持各种各样的HTTP动词和curl选项。这里有更多的使用文档。

免责声明:我是这个库的维护者。

cesanta的猫鼬库似乎也支持这一点:https://github.com/cesanta/mongoose/blob/6.17/examples/http_client/http_client.c

虽然有点晚了。你可能更喜欢https://github.com/Taymindis/backcurl。

它允许你在移动c++开发上进行http调用。适合手机游戏开发

bcl::init(); // init when using

bcl::execute<std::string>([&](bcl::Request *req) {
    bcl::setOpts(req, CURLOPT_URL , "http://www.google.com",
             CURLOPT_FOLLOWLOCATION, 1L,
             CURLOPT_WRITEFUNCTION, &bcl::writeContentCallback,
             CURLOPT_WRITEDATA, req->dataPtr,
             CURLOPT_USERAGENT, "libcurl-agent/1.0",
             CURLOPT_RANGE, "0-200000"
            );
}, [&](bcl::Response * resp) {
    std::string ret =  std::string(resp->getBody<std::string>()->c_str());
    printf("Sync === %s\n", ret.c_str());
});


bcl::cleanUp(); // clean up when no more using

注意,这并不需要libcurl, Windows.h,或WinSock!没有编译库,没有项目配置,等等。我有这段代码在Windows 10上的Visual Studio 2017 c++中工作:

#pragma comment(lib, "urlmon.lib")

#include <urlmon.h>
#include <sstream>

using namespace std;

...

IStream* stream;
//Also works with https URL's - unsure about the extent of SSL support though.
HRESULT result = URLOpenBlockingStream(0, "http://google.com", &stream, 0, 0);
if (result != 0)
{
    return 1;
}
char buffer[100];
unsigned long bytesRead;
stringstream ss;
stream->Read(buffer, 100, &bytesRead);
while (bytesRead > 0U)
{
    ss.write(buffer, (long long)bytesRead);
    stream->Read(buffer, 100, &bytesRead);
}
stream->Release();
string resultString = ss.str();

我只是想出了如何做到这一点,因为我想要一个简单的API访问脚本,像libcurl这样的库给我带来了各种各样的问题(即使我遵循了说明……),而WinSock只是太低级和复杂了。

我不太确定所有的IStream读取代码(特别是while条件-请随意纠正/改进),但嘿,它工作,麻烦!(这对我来说是有意义的,因为我使用了一个阻塞(同步)调用,这是很好的,bytesRead将始终是> 0U,直到流(ISequentialStream?)完成读取,但谁知道呢。)

请参见:URL名称和异步可插协议参考

libCURL是一个非常好的选择。根据你需要做什么,教程应该告诉你你想要什么,特别是简单的操作。但是,基本上,你可以这样做只是为了查看页面的源代码:

CURL* c;
c = curl_easy_init();
curl_easy_setopt( c, CURL_URL, "www.google.com" );
curl_easy_perform( c );
curl_easy_cleanup( c );

我相信这将导致结果被打印到标准输出。如果您想要处理它——我假设您是这样做的——您需要设置CURL_WRITEFUNCTION。上面链接的curl教程中介绍了所有这些内容。