如何使用C ++进行HTTP请求? [英] How do you make a HTTP request with C++?
问题描述
有没有办法轻松使用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 是另一个也支持webdav的有趣的C库。
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.
curlpp似乎自然如果你使用C ++。源分发中提供了许多示例。
要获取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://www.wikipedia.org"));
string asAskedInQuestion = os.str();
请参阅curlpp源分发中的 examples
目录,有很多更复杂的情况。
See the examples
directory in curlpp source distribution, there is a lot of more complex cases.
我2美分...
这篇关于如何使用C ++进行HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!