CH4
CH4

Reputation: 31

WinHttpSendRequest failed with error code 87

I'm trying to download a file from a server using WinHttpSendRequest but the result is 0 and error code is 87 (ERROR_INVALID_PARAMETER).

// Specify an HTTP server.
if (hSession)
        hConnect = WinHttpConnect( hSession, T2W((LPTSTR)tsDownloadServer.c_str()), wPort, 0 );
// tsDownloadServer = "xxx.xxx.xxx.xx:xxxx"

// Create an HTTP request handle.
if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"GET",T2W((LPTSTR)tsDownloadFileURLPath.c_str()), NULL, WINHTTP_NO_REFERER, ppwszAcceptTypes, dwOpenRequestFlag );
// tsDownloadFileURLPath = "/xxxxx/xxxxxxxx/58bbf9067ad35634c7caa5594e8ec712/windows_installer/xxxxx_xxxxxx.wak"

bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );

bResults is 0 and GetLastError() return 87

I researched on the internet about the related issue is due to exceed 24 bytes data however in my case, I set WINHTTP_NO_REQUEST_DATA in the parameter.

How can I send a request to the server?

Upvotes: 1

Views: 4451

Answers (1)

Drake Wu
Drake Wu

Reputation: 7190

ERROR_INVALID_PARAMETER means that the issue is on handler hRequest, which you have not checked the return value yet. If it is NULL, that maybe the cause was on the last call WinHttpOpenRequest; or even WinHttpConnect was failed, so that it didn't get hRequest any more.

As @Remy Lebeau said, it depends on your Unicode setting. With Unicode disabled, if the type of tsDownloadServer and tsDownloadFileURLPath is wstring, then (LPTSTR)tsDownloadServer.c_str() will be convert from wide char to multiple bytes. Then the zero in the wide character(If the character is in ASCII) is considered the terminator: enter image description here

And the string is truncated: enter image description here

Upvotes: 1

Related Questions