0

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

i am using gcc.exe (Rev2, Built by MSYS2 project) 12.1.0 to compile the program on a windows 10 machine but for some reason it is not working
I've updated my answer above. Your error might be caused by your Antivirus that inspects HTTPS connections, as you tried to connect to an HTTPS port using HTTP, it might have failed for that reason. I got an response from Cloudflare that I've been using the HTTPS port for HTTP so the code didn't fail on me.
this still does not work and I get this output HttpSendRequest Failed with error: 12029 Output:
You may try my pre-compiled .exe here: drive.google.com/file/d/1-4Wk5OITzIm6PHOGF1TWGXI6e8nKnTv0/… I've tried it on XP where I've got error 12157, and it should be working with Win 7+ (didn't test Vista) So there must be something going on with your PC like Antivirus, Proxy etc. I would suggest to try your code on a different PC, or alternatively within an Virtual Machine (that's generally helpful when you're writing and testing programs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.