C++ REST SDK(Casablanca)

C++ REST SDK(Casablanca),第1张

C++ REST SDK(Casablanca) 简介

微软开发了一个开源跨平台的http库--C++ REST SDK,又名卡萨布兰卡Casablanca。由于REST API的请求支持application/x-www-form-urlencoded、application/json、application/octet-stream等多种编码方式,REST API的返回值都是json形式,很方便返回对象。Casablanca采用c++11开发,集成了PPL和asio,支持异步数据流和web socket.

下载地址:https://github.com/Microsoft/cpprestsdk

头文件
#include 
#include 
#include 
#include 

using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;
相关对象和函数介绍 http_client_config

客户端配置信息,包含超时时间等

http_client_config m_ClientConfig;
m_ClientConfig.set_timeout(utility::seconds(10));
uri

统一资源标志符,本质上属于宽字符

uri m_Uri;
utility::string_t address = U("http://localhost:34568");//主机地址
m_Uri = http::uri(address);

资源连接地址会经常包含资源路径和其它参数,可以使用uri_builder进行拼接

uri_builder builder;
builder.append_path(L"search"); //添加URL
builder.append_query(L"q", L"Casablanca CodePlex"); //添加url参数
http_client

客户端,需要它发起http请求,构造函数如下

/// 
    /// Creates a new http_client connected to specified uri.
    /// 
    /// A string representation of the base uri to be used for all requests. Must start with
    /// either "http://" or "https://"
    _ASYNCRTIMP http_client(const uri& base_uri);

    /// 
    /// Creates a new http_client connected to specified uri.
    /// 
    /// A string representation of the base uri to be used for all requests. Must start with
    /// either "http://" or "https://" The http client configuration object
    /// containing the possible configuration options to initialize the http_client. 
    _ASYNCRTIMP http_client(const uri& base_uri, const http_client_config& client_config);
request

请求命令,最常用的格式如下:

/// 
/// Asynchronously sends an HTTP request.
/// 
/// HTTP request method.
/// String containing the path, query, and fragment, relative to the http_client's
 base URI. 
/// The data to be used as the message body, represented using the json object library. 
/// Cancellation token for cancellation of this request operation.
/// An asynchronous operation that is completed once a response from the request is received.

    pplx::task request(const method& mtd,
                                      const utility::string_t& path_query_fragment,
                                      const json::value& body_data,
                                      const pplx::cancellation_token& token =           pplx::cancellation_token::none()
                                      )
http_response

响应消息,包含结果和返回值

实例代码如下:
//HTTP客户端配置,用于设置创建http_client实例的可配置选项。
http_client_config m_ClientConfig;
m_ClientConfig.set_timeout(utility::seconds(30));
//uri参数定义
uri m_Uri;
utility::string_t address = U("http://localhost:34568");//主机地址
m_Uri = http::uri(address);
//增加一些URL,可以用uri_builder来做拼接
uri_builder builder;
builder.append_path(L"search"); //添加URL
builder.append_query(L"q", L"Casablanca CodePlex"); //添加url参数
//request body,一般是json或者二进制格式,此处是json
json::value reqMsg;
reqMsg[U("1")] = json::value::string(U("A"));
reqMsg[U("2")] = json::value::string(U("B"));
reqMsg[U("3")] = json::value::string(U("C"));
//客户端,发起http请求
http_client client(m_Uri, m_ClientConfig);
http_response response = client.request(methods::POST, builder.to_string(), reqMsg).get();//get()方法会阻塞等待异步线程完成 *** 作
//http_response对象来处理响应

if (response.status_code() == status_codes::OK)
{
	try
	{
		ucout << "responsePost: " << response.to_string() << std::endl;
		const json::value &jv = response.extract_json().get();
	}
	catch (const std::exception &e)
	{
		ucout << e.what() << std::endl;
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5703201.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存