tags:

views:

27

answers:

0

I'm sending a WinHttp request with POST data to a php script on an IIS7 server, and the POST body isn't being received by the server. If I send via WinHttp using GET, or POST with a NULL body, or through an HTML form using POST with a body, everything works as expected.

Here's some simple code showing the difference between by WinHttp POST calls with and without a 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);//WINHTTP_FLAG_ASYNC);
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 a body:

HINTERNET hSession = WinHttpOpen(L"WinHTTP/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);//WINHTTP_FLAG_ASYNC);
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);

So the only difference are the body parameters, and the content-type header. The really odd thing is that this might work 1 out of 20 times, but usually the body isn't received by the server and it times out. Anything obviously wrong here?