0

I'm trying to post data to a test .php script using XAMPP on my local machine, but it always fails with ERROR_HTTP_HEADER_NOT_FOUND. I check the server logs and no request was sent to Apache. I can manually do it via a web browser and the php script runs as expected.

Here's the C++ code that doesn't work. Can someone point out the problem?

static const char *accepttypes[]={ "application/x-www-form-urlencoded", NULL};
static const char headers[]="application/x-www-form-urlencoded\r\n";
    
// open session
HINTERNET hsession;
if ((hsession=InternetOpen(_T("Whatever"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0)) != NULL) {
    HINTERNET hconnect;
    if ((hconnect=InternetConnect(hsession, _T("127.0.0.1"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL) {
        HINTERNET hpostreq;
        if ((hpostreq=HttpOpenRequestA(hconnect, "POST", "/mytest.php", NULL, NULL, accepttypes, INTERNET_FLAG_RELOAD, 0)) != NULL) {
            BYTE *data=(BYTE*)"xyz=1234";
            DWORD datasize=8;
            if (HttpSendRequestA(hpostreq, headers, sizeof(headers), (LPVOID) data, datasize)) {
                ....
              
                // ^^^^^ That always fails with ERROR_HTTP_HEADER_NOT_FOUND
0

1 Answer 1

1

I see two mistakes in your call to HttpSendRequestA():

  • The value of headers needs to be changed from "application/x-www-form-urlencoded\r\n" to "Content-Type: application/x-www-form-urlencoded".

  • sizeof(headers) needs to be changed to either -1, sizeof(headers)-1, or strlen(headers). You are expected to pass in either -1 to let the function calculate the character length of the headers string, or you can pass in the actual character length. The string's null terminator is not to be included in that length.

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

1 Comment

It was the sizeof()-1 that was needed (I wondered about that). The Content-Type was missing because when I changed from octet-stream back to to the urlencoded I had commented out, I missed it on a copy/paste (ugh). Thanks!!

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.