WinHttpSendRequest失败,错误代码为87

时间:2019-04-01 16:41:11

标签: windows c++11 winapi

我正在尝试使用WinHttpSendRequest从服务器下载文件,但结果为0,错误代码为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为0,GetLastError()返回87

我在互联网上研究了相关问题,原因是数据超过24个字节,但是在我的情况下,我在参数中设置了WINHTTP_NO_REQUEST_DATA。

如何将请求发送到服务器?

1 个答案:

答案 0 :(得分:1)

ERROR_INVALID_PARAMETER表示问题出在处理程序hRequest上,您尚未检查返回值。如果为NULL,则可能是上次调用WinHttpOpenRequest造成的;甚至WinHttpConnect都失败了,因此不再得到hRequest

正如@Remy Lebeau所说,这取决于您的Unicode设置。在禁用Unicode的情况下,如果tsDownloadServertsDownloadFileURLPath的类型为wstring,则(LPTSTR)tsDownloadServer.c_str()将从宽字符转换为多个字节。然后,将宽字符中的零(如果字符为ASCII)视为终止符: enter image description here

并且字符串被截断: enter image description here