且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

你如何用 C++ 发出 HTTP 请求?

更新时间:2022-04-05 06:21:44

我遇到了同样的问题.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 ...