wchar_t buf[48] = {};
http_response response = client.request(methods::POST, builder.to_string(), buf/*L""*/, L"application/octet-stream").get();
请求发起之后就等http响应了,rest api返回的结果都是json格式的,所以我们需要解析json对象,rest sdk提供了http_response对象来处理响应。假设http响应的结果是这样的:
?
{
? ? "result":"service failed"
? ? "error_code": 400
}
http响应的处理:
?
复制代码
if (response.status_code() == status_codes::OK)
{
? ? try
? ? {
? ? ? ? result = true;
? ? ? ? const json::value& jv = response.extract_json().get();
? ? ? ? const web::json::object& jobj = jv.as_object();
? ? ? ? auto result = jobj.at(L"result").as_string();
? ? ? ? auto access_code = result.as_object().at(L"error_code").as_string();
? ? ? ? wcout << result<<" "<< access_code << endl;
? ? }
? ? catch (const std::exception& e)
? ? {
? ? ? ? cout << e.what() << endl;
? ? }
}
复制代码
用wcout输出宽字符时需要做一个初始化,否则可能输出不了内容。
?
wcout.imbue(locale("chs"));//本地化
我们还可以设置相关的http属性,http_client默认的超时时间是30秒,我们也可以自己设置超时时间:
?
http_client_config config;
config.set_timeout(utility::seconds(90)); //设置为90秒超时
http_client client(URL, config);
?