Reputation: 344
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
Upvotes: 0
Views: 879
Reputation: 56
in your example, HttpSendRequest
should actually not supply hdrs
and strlen(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 connection
Note²: your if
after InternetConnect()
is wrong, you still check for hSession
but you should check for hConnect
You can try this code, it should output "Bad Gateway" (This is the response from the Webserver)
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
#include <wininet.h>
int main()
{
BOOL success;
HINTERNET hConnect, hSession, hRequest;
const TCHAR* hdrs = _T("Content-Type: application/json");
TCHAR output[4096];
DWORD read;
const TCHAR* rgpszAcceptTypes[] = {_T("text/*"), NULL};
hSession = InternetOpen(_T(""),INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
if (hSession == NULL)
printf("InternetOpen Failed with error: %ld\n", GetLastError());
hConnect = InternetConnect(hSession, _T("requestinspector.com"), INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect == NULL)
printf("InternetConnect Failed with error: %ld\n", GetLastError());
hRequest = HttpOpenRequest(hConnect, NULL, _T("/inspect/<id>"), NULL,NULL,rgpszAcceptTypes, INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_SECURE, 0);
if (hRequest == NULL)
printf("HttpOpenRequest Failed with error: %ld\n", GetLastError());
success = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), NULL,0);
if (!success)
printf("HttpSendRequest Failed with error: %ld\n", GetLastError());
success = InternetReadFile(hRequest, output, (sizeof(output)/sizeof(output[0]))-1, &read);
if (!success) {
printf("InternetReadFile Failed with error: %ld\n", GetLastError());
return -1;
}
output[read] = '\0';
printf("Output:\n%s\n\n", output);
}
I've only corrected the flags passed to InternetConnect()
(should have none) and HttpOpenRequest()
which was missing INTERNET_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 use INTERNET_FLAG_IGNORE_CERT_CN_INVALID
and changed the code to use _T()
for Unicode support more often. (you only used it for rgpszAcceptTypes
)
Upvotes: 1