WinHttp POST body not received

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);//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 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);

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?

+5
source share
1 answer

, , PUT POST.

WinHttp VFP, . POST, , , WinHttp, .

PUT, , ... ,

// 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);

, .

, POST ASP.NET. PHP/Linux.

+2

All Articles