I am sending a WinHttp request with POST data to a php script on an IIS7 server, and the POST body is not accepted by the server. If I send via WinHttp using GET or POST with a NULL body or via an HTML form using POST with a body, everything will work as expected.
Here is some simple code showing the difference between WinHttp POST calls with and without the body:
Without a body:
HINTERNET hSession = WinHttpOpen(L"WinHTTP/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
HINTERNET mConnection = WinHttpConnect(hSession, L"127.0.0.1", 80, 0);
HINTERNET hRequest = WinHttpOpenRequest(mConnection, L"POST", L"/test.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
void* bodyData = NULL;
DWORD bodyLength = 0;
bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, bodyData, bodyLength, bodyLength, 0);
With body:
HINTERNET hSession = WinHttpOpen(L"WinHTTP/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
HINTERNET mConnection = WinHttpConnect(hSession, L"127.0.0.1", 80, 0);
HINTERNET hRequest = WinHttpOpenRequest(mConnection, L"POST", L"/test.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
char* pBodyStr = "a=1&b=2";
void* bodyData = (void*) pBodyStr;
DWORD bodyLength = strlen(pBodyStr);
bResult = WinHttpSendRequest(hRequest, L"content-type:application/x-www-form-urlencoded", -1, bodyData, bodyLength, bodyLength, 0);
Thus, the only difference is the body parameters and the content type header. In fact, it is strange that this can work 1 out of 20 times, but usually the body does not receive the server, and this time expires. Is something obviously wrong here?