【问题标题】:CPPRestSdk giving error SSL Error: WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA SSL invalid CA for HTTPS connectionCPPrestSdk 给出错误 SSL 错误:WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA SSL HTTPS 连接无效 CA
【发布时间】:2019-11-12 08:04:25
【问题描述】:

我在运行一个需要 REST 客户端连接到 REST 服务器的旧项目时遇到错误。 我的客户端是用微软的 cpprestsdk 项目实现的。

我的网址是这样的;

https://localhost:5276/Command

我收到了错误:

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


    【解决方案1】:

    为了解决这个问题,我将客户端对象创建行从

    http_client client(U("https://localhost:5276/Command"));
    

    http_client_config config;
    config.set_validate_certificates(false);
    http_client client(U("https://localhost:5276/Command"), config);
    

    【讨论】:

    • 有趣的方法。坦率地说,我质疑故意不验证对等点(CA 信任)或主机(DN 和/或 SAN 验证)的信任以获取 REST API 之类的东西,除非它恰好处于您所处的条件:本地服务您有效地使用 REST 作为 RPC 机制。所以...买家要小心。
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 2011-03-18
    • 2012-11-18
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多