CPPRestSdk giving error SSL Error: WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA SSL invalid CA for HTTPS connection

I was getting an error when I ran an old project which required my REST client to connect with a REST Server. My client was implemented with the Microsoft’s cpprestsdk project.

My URL was of the form;

https://localhost:5276/Command

I was getting the error:

SSL Error: WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA SSL invalid CA. WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID SSL common name does not match. WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID SLL certificate is expired.

Rest Client code:

#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;
}

At the time of this writing, I have taken the code sample from the Marius Bancila's Blog.

I used cpprestsdk as a nugetpackage from visual studio. The current version of the package is 2.10.12.1

Upvotes: 0

Views: 3581

Answers (1)

To fix this, I changed the client object creating line from

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

to

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

Upvotes: 1

Related Questions