Can't get HTTP POST request to work properly

2019-05-22 00:07发布

I'm trying to send a file from a game I'm working on to the game's website using an HTTP POST request. When I send the file using a form on an HTML page, it works fine. However, I have been unable to simulate the same thing inside the game. I am receiving a response code of 200 indicating that the request went successfully, however, the upload script indicates that it received a GET request, and the $_FILES and $_POST arrays are empty. I have looked at the output of both the HTML form's POST and the game's POST in Wireshark, but have been unable to detect any meaningful differences between the two. (If I knew how to duplicate the HTML form's POST method exactly, I would, but I don't think that's possible.)

Anyway, here's the code that I'm using to send it. Thanks!

HINTERNET           hInternet;
HINTERNET           hConnect;
HINTERNET           hRequest;
INTERNET_BUFFERS    Buffers;
DWORD               dwBytesWritten;
DWORD               dwNumBytesToWrite;
char                szBoundary[32];
char                szContentTypeHeader[64];
char                szFileHeader[256];
char                szFileFooter[4];
char                szBodyFooter[32];
ULONG               ulTotalBytesWritten;
ULONG               ulOffset;
ULONG               ulContentLength;

// Get a handle for working with the Internet.
hInternet = InternetOpen( "Wrack", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if ( hInternet == NULL )
    return ( false );

// Open an HTTP session for the site.
hConnect = InternetConnect( hInternet, "wrackgame.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL );
if ( hConnect == NULL )
{
    InternetCloseHandle( hInternet );

    return ( false );
}

// Open the POST request.
hRequest = HttpOpenRequest( hConnect, "POST", "leaderboard/upload_file.php", NULL, "http://www.wrackgame.com/leaderboard/upload.html", NULL, 0, NULL );
if ( hRequest == NULL )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );

    return ( false );
}

// Generate our various headers and footers.
sprintf_s( szBoundary, "----%04x%04x%04x", rand( ) % 0xffff, rand( ) % 0xffff, rand( ) % 0xffff );
sprintf_s( szContentTypeHeader, "Content-Type: multipart/form-data; boundary=%s", szBoundary );
sprintf_s( szFileHeader, "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n", szBoundary, "file", "testreplay.wrp" );
sprintf_s( szFileFooter, "\r\n" );
sprintf_s( szBodyFooter, "--%s--\r\n", szBoundary );

// Build our header.
if ( HttpAddRequestHeaders( hRequest, szContentTypeHeader, -1, HTTP_ADDREQ_FLAG_ADD ) == false )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );
    InternetCloseHandle( hRequest );

    return ( false );
}

// Calculate how much data we'll be sending.
ulContentLength = network_CalcContentLength( szFileHeader, szFileFooter, szBodyFooter );

// Initialize our buffers.
memset( &Buffers, 0, sizeof( INTERNET_BUFFERS ));
Buffers.dwStructSize = sizeof( INTERNET_BUFFERS );
Buffers.dwBufferTotal = ulContentLength;

// Send our HTTP request.
if ( HttpSendRequestEx( hRequest, &Buffers, NULL, HSR_INITIATE, NULL ) == false )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );
    InternetCloseHandle( hRequest );

    return ( false );
}

// Send the header.
ulTotalBytesWritten = 0;
if ( InternetWriteFile( hRequest, szFileHeader, strlen( szFileHeader ), &dwBytesWritten ) == false )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );
    InternetCloseHandle( hRequest );

    return ( false );
}
ulTotalBytesWritten += dwBytesWritten;

// Next, write the body of the replay.
ulOffset = 0;
while ( ulOffset < (DWORD)REPLAY_GetReplaySize( ))
{
    // Determine how many bytes of the replay to send. If we're almost
    // done, send less than 1024 bytes.
    dwNumBytesToWrite = min( 1024, REPLAY_GetReplaySize( ) - ulOffset );

    // Send a piece of the replay and log how many bytes we actually
    // transferred.
    if ( InternetWriteFile( hRequest, REPLAY_GetReplayData( ) + ulOffset, dwNumBytesToWrite, &dwBytesWritten ) == false )
    {
        InternetCloseHandle( hInternet );
        InternetCloseHandle( hConnect );
        InternetCloseHandle( hRequest );

        return ( false );
    }
    ulTotalBytesWritten += dwBytesWritten;

    // Increment the offset of the replay buffer.
    ulOffset += 1024;
}

// Send our file footer.
if ( InternetWriteFile( hRequest, szFileFooter, strlen( szFileFooter ), &dwBytesWritten ) == false )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );
    InternetCloseHandle( hRequest );

    return ( false );
}
ulTotalBytesWritten += dwBytesWritten;

// Send our body footer.
if ( InternetWriteFile( hRequest, szBodyFooter, strlen( szBodyFooter ), &dwBytesWritten ) == false )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );
    InternetCloseHandle( hRequest );

    return ( false );
}
ulTotalBytesWritten += dwBytesWritten;

// Close our request now that we're done.
if ( HttpEndRequest( hRequest, NULL, 0, NULL ) == false )
{
    InternetCloseHandle( hInternet );
    InternetCloseHandle( hConnect );
    InternetCloseHandle( hRequest );

    return ( false );
}

InternetCloseHandle( hInternet );
InternetCloseHandle( hConnect );
InternetCloseHandle( hRequest );

// No need to do anything more!
return ( true );

标签: c winapi http post
2条回答
做自己的国王
2楼-- · 2019-05-22 00:47

I tried to answer this earlier, but I guess it didn't get through. Anyway, I feel pretty silly. It's working perfectly now. The reason it wasn't working is because I had the second parameter of InternetOpen() as "wrackgame.com", instead of "www.wrackgame.com". I have no idea why that prevented it from working - especially considering that my upload script, /leaderboard/upload_file.php, was being executed. It's not like the traffic wasn't reaching the server. Strange.

Anyway, thanks for the help everyone!

查看更多
Fickle 薄情
3楼-- · 2019-05-22 00:54

I'm not familiar with the WinINet lib, but libcurl has a c interface including some easy to follow examples on how to setup a HTTP POST request

查看更多
登录 后发表回答