Curl for People (crp)库介绍

1、编译

crp库是在curl的基础上封装的一个使用便捷的库,linux下编译cmake比较简单。在windows下编译步骤有几点注意的地方,下面的编译会有提示。(使用版本:cpr-1.3.0,curl-7.59.0)
首先下载源码包,https://github.com/whoshuu/cpr/releases,下载之后需要在opt目录下放入依赖库 curl、googletest、mongoose。这里简单起见,我们仅放入curl的源码。
这里写图片描述
编译时默认是使用googletest、mongoose,查看当前opt目录的CMakeFlie发现
去掉”BUILD_CPR_TESTS”即可。

另外,我们使用curl访问https时会可能会出现Protocol https not supported or disabled in libcurl错误提示,需要导入openssl库或者其他替代方案。

这里用的windows,方便起见,不使用openssl,因此使用winssl,并且允许NTMS授权,即需要在cmake时关闭“CMAKE_USE_OPENSSL”,开启
“CMAKE_USE_WINSSL”和“CURL_WINDOWS_SSPI”。编译后在lib(截图为debug)文件夹:
这里写图片描述
项目使用(debug)时,包含目录为 “xx\cpr-1.3.0\include”,库目录为“xx\cpr-1.3.0\build\lib\Debug”,依赖项有需要两个cpr-d.lib和libcurl-d_imp.lib,运行使用libcurl-d.dll。

2、设计

C ++请求被设计为尽可能简单和愉快地使用。通过主API的HTTP方法调用是短暂且无状态的 - 该库坚信资源获取是初始化。清理和让对象脱离范围一样简单直观。你不会在这里找到任何init()或close()方法,正确使用这个库既不需要delete也不需要free。

在cpr中,选项实际上是选项,所以如果你不设置它们,它们将默认为合理的值。这通过关键字args-like界面来促进:

auto r = cpr::Get(cpr::Url{"http://www.httpbin.org/get"},
                  cpr::Parameters{{"hello", "world"}}); // Url object before Parameters

和下面的代码是完全一样的

auto r = cpr::Get(cpr::Parameters{{"hello", "world"}},
                  cpr::Url{"http://www.httpbin.org/get"}); // Parameters object before Url

也就是说,选项的顺序完全是任意的,你可以将它们放在任何你想要的地方,并且传出的呼叫是相同的。这就是它!当然,有很多不同的方式来配置你的请求,但是通过设计他们都可以通过相同的简单界面进行访问。

3、GET请求

使用cpr发出GET请求毫不费力:

#include <cpr/cpr.h> // Make sure this header is available in your include path

// Somewhere else
auto r = cpr::Get(cpr::Url{"http://www.httpbin.org/get"});

这给了我们一个Response我们所称的对象r。那里有很多好东西:

std::cout << r.url << std::endl; // http://www.httpbin.org/get
std::cout << r.status_code << std::endl; // 200
std::cout << r.header["content-type"] << std::endl; // application/json
std::cout << r.text << std::endl;

/*
 * {
 *   "args": {},
 *   "headers": {
 *     ..
 *   },
 *   "url": "http://httpbin.org/get"
 * }
 */

要添加URL编码参数,请将Parameters对象引入Get呼叫:

auto r = cpr::Get(cpr::Url{"http://www.httpbin.org/get"},
                  cpr::Parameters{{"hello", "world"}});
std::cout << r.url << std::endl; // http://www.httpbin.org/get?hello=world
std::cout << r.text << std::endl;

/*
 * {
 *   "args": {
 *     "hello": "world"
 *   },
 *   "headers": {
 *     ..
 *   },
 *   "url": "http://httpbin.org/get?hello=world"
 * }
 */

Parameters是一个类似于map的对象。您可以使用Get方法内部的键/值对列表构造它,或者Get通过在外部构造它,

// Constructing it in place
auto r = cpr::Get(cpr::Url{"http://www.httpbin.org/get"},
                  cpr::Parameters{{"hello", "world"}, {"stay", "cool"}});
std::cout << r.url << std::endl; // http://www.httpbin.org/get?hello=world&stay=cool
std::cout << r.text << std::endl;

/*
 * {
 *   "args": {
 *     "hello": "world"
 *     "stay": "cool"
 *   },
 *   "headers": {
 *     ..
 *   },
 *   "url": "http://httpbin.org/get?hello=world&stay=cool"
 * }
 */

 // Constructing it outside
auto parameters = cpr::Parameters{{"hello", "world"}, {"stay", "cool"}};
auto r_outside = cpr::Get(cpr::Url{"http://www.httpbin.org/get"}, parameters);
std::cout << r_outside.url << std::endl; // http://www.httpbin.org/get?hello=world&stay=cool
std::cout << r_outside.text << std::endl; // Same text response as above

到目前为止,很多示例都在方法调用中构造了可选的对象,但对这些对象的来源没有限制。该库足够聪明,可以知道何时必须复制参数以及何时可以安全地从调用处移动对象。

4、POST请求

发出POST请求与GET请求一样简单:

auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Payload{{"key", "value"}});
std::cout << r.text << std::endl;

/*
 * {
 *   "args": {},
 *   "data": "",
 *   "files": {},
 *   "form": {
 *     "key": "value"
 *   },
 *   "headers": {
 *     ..
 *     "Content-Type": "application/x-www-form-urlencoded",
 *     ..
 *   },
 *   "json": null,
 *   "url": "http://www.httpbin.org/post"
 * }
 */

在POST请求中”key=value”作为一个”x-www-form-urlencoded”对发送。要发送原始和未编码的数据,使用Body的不是Payload:

auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Body{"This is raw POST data"},
                   cpr::Header{{"Content-Type", "text/plain"}});
std::cout << r.text << std::endl;

/*
 * {
 *   "args": {},
 *   "data": "This is raw POST data",
 *   "files": {},
 *   "form": {},
 *   "headers": {
 *     ..
 *     "Content-Type": "text/plain",
 *     ..
 *   },
 *   "json": null,
 *   "url": "http://www.httpbin.org/post"
 * }
 */

在这里您会注意到”Content-Type”被明确设置”text/plain”。这是因为默认情况下,”x-www-form-urlencoded”用于原始数据POST。这对于发送数据较小、”x-www-form-urlencoded”或”text/plain的情况是合适的。如果数据包很大或包含文件,则使用Multipart上传更合适:

auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Multipart{{"key", "large value"},
                                  {"name", cpr::File{"path-to-file"}}});
std::cout << r.text << std::endl;

/*
 * {
 *   "args": {},
 *   "data": "",
 *   "files": {
 *     "name": <contents of file>
 *   },
 *   "form": {
 *     "key": "large value"
 *   },
 *   "headers": {
 *     ..
 *     "Content-Type": "multipart/form-data; boundary=--------------33b210e9d7b8bd02",
 *     ..
 *   },
 *   "json": null,
 *   "url": "http://www.httpbin.org/post"
 * }
 */

注意”Content-Type”返回头中的内容现在是不同的; 这与之”multipart/form-data”相反”x-www-form-urlencoded”。这有助于使用POST进行更大和更通用的数据上传。

如果文件的内容已经在内存中,也可以传递缓冲区而不是文件名。

// STL containers like vector, string, etc.
std::vector<char> content{'t', 'e', 's', 't'};
auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Multipart{{"key", "large value"},
                                  {"name", cpr::Buffer{content.begin(), content.end(), "filename.txt"}}});

// C-style pointers
const char *content = "test";
int length = 4;
auto r = cpr::Post(cpr::Url{"http://www.httpbin.org/post"},
                   cpr::Multipart{{"key", "large value"},
                                  {"name", cpr::Buffer{content, content + length, "filename.txt"}}});

5、认证

任何自我尊重的网络库都应该支持认证。web API允许不受限制地访问他们所保护的数据集是很少见的。他们通常需要一个用户名/密码对:

auto r = cpr::Get(cpr::Url{"http://www.httpbin.org/basic-auth/user/pass"},
                  cpr::Authentication{"user", "pass"});
std::cout << r.text << std::endl;

/*
 * {
 *   "authenticated": true,
 *   "user": "user"
 * }
 */

这使用基本身份验证。要使用摘要式身份验证,只需使用Digest身份验证对象:

auto r = cpr::Get(cpr::Url{"http://www.httpbin.org/digest-auth/auth/user/pass"},
                  cpr::Digest{"user", "pass"});
std::cout << r.text << std::endl;

/*
 * {
 *   "authenticated": true,
 *   "user": "user"
 * }
 */

通过这些基本操作,现代C ++可以访问绝大部分的万维网API。对于更复杂的应用程序,请查看高级使用指南(Curl for People (crp)库介绍——高级用法 ).

猜你喜欢

转载自blog.csdn.net/wanggao_1990/article/details/79805476
今日推荐