你如何用 C++ 发出 HTTP 请求? [英] How do you make a HTTP request with C++?

查看:50
本文介绍了你如何用 C++ 发出 HTTP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以轻松地使用 C++ 发出 HTTP 请求?具体来说,我想下载一个页面(一个API)的内容,并检查内容是否包含1或0.是否也可以将内容下载为字符串?

Is there any way to easily make a HTTP request with C++? Specifically, I want to download the contents of a page (an API) and check the contents to see if it contains a 1 or a 0. Is it also possible to download the contents into a string?

推荐答案

我遇到了同样的问题.libcurl 真的很完整.有一个 C++ 包装器 curlpp 当您要求 C++ 库时,您可能会对它感兴趣.neon 是另一个有趣的 C 库,它也支持 WebDAV.

I had the same problem. libcurl is really complete. There is a C++ wrapper curlpp that might interest you as you ask for a C++ library. neon is another interesting C library that also support WebDAV.

如果您使用 C++,curlpp 看起来很自然.源代码分发中提供了许多示例.要获取 URL 的内容,您可以执行类似操作(从示例中提取):

curlpp seems natural if you use C++. There are many examples provided in the source distribution. To get the content of an URL you do something like that (extracted from examples) :

// Edit : rewritten for cURLpp 0.7.3
// Note : namespace changed, was cURLpp in 0.7.2 ...

#include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp>

// RAII cleanup

curlpp::Cleanup myCleanup;

// Send request and get a result.
// Here I use a shortcut to get it in a string stream ...

std::ostringstream os;
os << curlpp::options::Url(std::string("http://example.com"));

string asAskedInQuestion = os.str();

查看curlpp源码分发中的examples目录,有很多更复杂的案例,以及简单完整的最小案例使用 curlpp.

See the examples directory in curlpp source distribution, there is a lot of more complex cases, as well as a simple complete minimal one using curlpp.

我的 2 美分......

my 2 cents ...

这篇关于你如何用 C++ 发出 HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆