Why does this function result in HttpSendRequest failing to send the request
int temp()
{
BOOL success;
HINTERNET hConnect, hSession, hRequest;
char hdrs[] = "Content-Type: application/json";
PCTSTR rgpszAcceptTypes[] = {_T("text/*"), NULL};
hSession = InternetOpen("",INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
if (hSession == NULL)
printf("InternetOpen Failed with error: %ld\n", GetLastError());
hConnect = InternetConnect(hSession, "requestinspector.com", INTERNET_DEFAULT_HTTPS_PORT, NULL,NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_IGNORE_CERT_CN_INVALID, 0);
if (hSession == NULL)
printf("InternetConnect Failed with error: %ld\n", GetLastError());
hRequest = HttpOpenRequest(hConnect, NULL, "/inspect/<id>", NULL,NULL,rgpszAcceptTypes, INTERNET_FLAG_IGNORE_CERT_CN_INVALID, 0);
if (hRequest == NULL)
printf("HttpOpenRequest Failed with error: %ld\n", GetLastError());
success = HttpSendRequest(hRequest, hdrs,strlen(hdrs), NULL,0);
if (!success)
printf("HttpSendRequest Failed with error: %ld\n", GetLastError());
}
I am completely clueless to why this is failing to send the request
the output
gcc http.c -lwininet && ./a.exe
HttpSendRequest Failed with error: 12029
in your example,
HttpSendRequestshould actually not supplyhdrsandstrlen(hdrs)as you're just doing a GET Request and not a POST.However, the code itself would work without your given error (on my side with Windows 10)
Error 12029 suggests that an HTTPS connection couldn't be established and I assume you're either using an outdated compiler or something like Windows XP which doesn't support the latest encryption standards. However, the website in your example accepts TLS 1.0 to 1.3 and only SSL is disabled. But it could also be certificate related.
Note: the connections really is started by
HttpSendRequest(). The function calls before that only define the details but won't establish the connectionNote²: your
ifafterInternetConnect()is wrong, you still check forhSessionbut you should check forhConnectYou can try this code, it should output "Bad Gateway" (This is the response from the Webserver)
I've only corrected the flags passed to
InternetConnect()(should have none) andHttpOpenRequest()which was missingINTERNET_FLAG_SECURE. So it didn't even try to use HTTPS and send HTTP data to the HTTPS port. Other than that, I wouldn't useINTERNET_FLAG_IGNORE_CERT_CN_INVALIDand changed the code to use_T()for Unicode support more often. (you only used it forrgpszAcceptTypes)