WinHttp POST body not received
Asked Answered
F

2

6

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?

Furlani answered 24/9, 2010 at 22:38 Comment(0)
C
2

For anyone else who still has this problem, try using PUT instead of POST.

In my case, the WinHttp client was in VFP, but the results were the same. A POST sent through a browser worked fine, but when I sent from the WinHttp object, the request body appeared empty.

PUT, however, worked normally... except that

// Simply using
// 
//    file_get_contents('php://input')
//    
// does not work with the request sent by WinHttp.WinHttpRequest.
$fp = fopen('php://input', 'rb');
stream_filter_append($fp, 'dechunk', STREAM_FILTER_READ);
$report_contents = stream_get_contents($fp);

Again, this was not an issue in browser-based requests.

Also, the POST worked just fine for an ASP.NET client. This only came up when trying to read it on a PHP/Linux page.

Chace answered 27/8, 2012 at 6:56 Comment(0)
P
0

I found that this behavior can occur when the data pointed to by bodyData isn't always kept alive long enough for WinHTTP to finish sending the data. This caused the server to sometimes receive the data just fine, while some other times the POST body would be empty.

Philippians answered 19/7 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.