【发布时间】:2019-11-12 08:04:25
【问题描述】:
我在运行一个需要 REST 客户端连接到 REST 服务器的旧项目时遇到错误。 我的客户端是用微软的 cpprestsdk 项目实现的。
我的网址是这样的;
我收到了错误:
SSL 错误:WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA SSL 无效 CA。 WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID SSL 公用名不 匹配。 WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID SLL 证书 已过期。
休息客户端代码:
#include <cpprest/http_client.h>
#include <cpprest/json.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include <iostream>
using namespace std;
void display_json(
json::value const & jvalue,
utility::string_t const & prefix)
{
wcout << prefix << jvalue.serialize() << endl;
}
pplx::task<http_response> make_task_request(
http_client & client,
method mtd,
json::value const & jvalue)
{
return (mtd == methods::GET || mtd == methods::HEAD) ?
client.request(mtd, L"/restdemo") :
client.request(mtd, L"/restdemo", jvalue);
}
void make_request(
http_client & client,
method mtd,
json::value const & jvalue)
{
make_task_request(client, mtd, jvalue)
.then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
display_json(previousTask.get(), L"R: ");
}
catch (http_exception const & e)
{
wcout << e.what() << endl;
}
})
.wait();
}
int main()
{
http_client client(U("https://localhost:5276/Command"));
auto getvalue = json::value::object();
getvalue[L"First"] = json::value::string(L"First Value");
getvalue[L"Second"] = json::value::string(L"Second Value");
getvalue[L"Third"] = json::value::number(3);
wcout << L"\nPOST (get some values)\n";
display_json(getvalue, L"S: ");
make_request(client, methods::POST, getvalue);
return 0;
}
在撰写本文时,我已从Marius Bancila's Blog 获取代码示例。
我使用 cpprestsdk 作为 Visual Studio 的 nugetpackage。包的当前版本是2.10.12.1
【问题讨论】:
标签: c++ rest https cpprest-sdk