Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1787

HttpSendRequest fails

$
0
0

I was just working on a project, and I required a c++ code in which I could implement wininet functions to make an HTTPS request to a server. Here is the following code ...

void connect(LPCSTR hostname, LPCSTR endpoint) // hostname = "127.0.0.1", endpoint = "/"{    LPCWSTR userAgent = L"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0";    HINTERNET hInternet = InternetOpenW(userAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);    if (hInternet == NULL) {        perror("InternetOpenW");        exit(EXIT_FAILURE);    }    HINTERNET hConnect = InternetConnect(hInternet, hostname, 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);    if (hConnect == NULL) {        InternetCloseHandle(hInternet);        perror("InternetConnect");        exit(EXIT_FAILURE);    }    PCTSTR accept[] = {"application/json", NULL};    HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", endpoint, "HTTP/1.1", NULL, accept, INTERNET_FLAG_SECURE, 0);    if (hRequest == NULL) {        InternetCloseHandle(hInternet);        InternetCloseHandle(hConnect);        perror("HttpOpenRequest");        exit(EXIT_FAILURE);    }    LPCSTR headers = "Content-Type: application/json\r\n";    if (HttpAddRequestHeaders(hRequest, headers, -1L, HTTP_ADDREQ_FLAG_ADD) == false) {        InternetCloseHandle(hInternet);        InternetCloseHandle(hConnect);        InternetCloseHandle(hRequest);        perror("HttpAddRequestHeaders");        exit(EXIT_FAILURE);    }    std::string data = "{\"sessionId\": \"importants.txt\", \"authToken\": \"Nothing Important Here.\"}";    if (HttpSendRequest(hRequest, NULL, -1L, (LPVOID) data.c_str(), data.length()) == false) {        InternetCloseHandle(hInternet);        InternetCloseHandle(hConnect);        InternetCloseHandle(hRequest);        perror("HttpSendRequest");        exit(EXIT_FAILURE);    }    std::string response = "";    char buffer[4096];    DWORD bytesRead;    while (InternetReadFile(hRequest, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead > 0) {        buffer[bytesRead] = '\0';        response += buffer;    }    std::cout << response << std::endl;    InternetCloseHandle(hInternet);    InternetCloseHandle(hConnect);    InternetCloseHandle(hRequest);}

But when I try to compile it, it compiles well. Here's the command in case ...

x86_64-w64-mingw32-g++ harvester.cpp -o harvester.exe -l wininet -static-libstdc++ -static-libgcc

But when I try to run it using wine, it outputs something like this and exits ...

$ wine harvester.exeMESA-INTEL: warning: Haswell Vulkan support is incompleteMESA-INTEL: warning: Haswell Vulkan support is incompleteHttpSendRequest: Success

My flask server at this point haven't even received a connection request, or else it would print a debug message. But if I try receiving the request using netcat, it shows something is received, but it's gibberish ...

$ nc -nvlp 443listening on [any] 443 ...connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 54286��A���3�fz�T�� J�M�_v��~�O�}�2�,̩����+����0̨��/����5���/�̪��9���3x�@"   127.0.0.1#^C

And then again the client exits with the previous message.

I can't understand where the bug is :(


Viewing all articles
Browse latest Browse all 1787

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>