Running into "error in WinHttpSendRequest" when making https requests

May 30, 2014 at 9:13 AM

I use casablanca to build a cpp rest client. Recently we need to replace all http requests with https requests. So I simply changed the "http" in the following code to "https":

        http_client_config config1;
        credentials cred = credentials(U("ADMIN"),U("ADMIN"));

        http_client session(U(""), config1);
        http_request request;
        std::string requestBody = "{\"abc\" : \"efg\"}";

        request.set_body(requestBody, L"application/json");

            http_response response = session.request(request).get();
        catch(std::exception& e)
I downloaded the lastest source code on the official website to run the above query. an exception is thrown at the try clause, and it prints "error in WinHttpSendRequest" I'm wondering what is the cause of this problem? My code works for non-http situations.

thanks very much!
May 30, 2014 at 9:19 AM
Edited May 30, 2014 at 9:20 AM
i know directly requesting https// without OAUTH won't work. but it's simply a example to show my problem on HTTPS. When i made the same request in REST client tools like POSTMAN, at least i can get a 401 error and {"error": "Invalid OAuth request."}, but why it throws exception when I use casablanca?
May 30, 2014 at 4:33 PM
Hi subuliu,

I have tested your sample code in my local machine with cpprestsdk2.0.1, win8.1, vs2013; it works correctly.

The error message "error in WinHttpSendRequest" means there is winhttp api system error.

Can you set "auto error = GetLastError();" after your "session.request(request).get();" statement? so we can understand what is the system error code.

Jun 2, 2014 at 1:13 PM
thanks very much for your reply, zhelu,

I was working on my company's windows 7, vs 2012, cpprestsdk2.0.1.
Inspired by your successful trial on your local machine. I tested again on my home PC again, which is also win7,vs2012 and cpprest2.0.1.
It's interesting that the code works on my home PC ,too.

So i guess it got to be a company firewall issue or something.
Still I wanna to locate the exact cause.
(The company PC's firewall is out of my control, so it's best if i could first identify the root reason)

I traced into the code and it was error:

The connection with the server has been reset or terminated, or an incompatible SSL protocol was encountered. For example, WinHTTP version 5.1 does not support SSL2 unless the client specifically enables it. (From MSDN)

However it's quite vague for me.
Since both my company PC and home PC are using the similar versions of SSL and WinHTTP,
The cause might only be connection reset or termination, but who could do it? The firewall?
Is there any rules that i should apply to the firewall?

You're an expert in this, is there any clue?

Jun 3, 2014 at 3:08 AM
hi, zhelu,

please save your time on this,
it turns out to be a network issue caused by the notorious GFW of CHINA.

Marked as answer by zhelu on 6/3/2014 at 8:31 AM