WinHttpSendRequest 和 ERROR_WINHTTP_RESEND_REQUEST

WinHttpSendRequest and ERROR_WINHTTP_RESEND_REQUEST

调用 WinHttpSendRequest 后 GetLastError() 是否可以 return ERROR_WINHTTP_RESEND_REQUEST?

WinHttpSendRequest 的文档:

ERROR_WINHTTP_RESEND_REQUEST
The application must call WinHttpSendRequest again due to a redirect or authentication challenge. Windows Server 2003 with SP1, Windows XP with SP2 and Windows 2000:  This error is not supported.

但来自 MSDN 的示例(WinHTTP 中的身份验证)会在 WinHttpReceiveResponse 之后检查此值。

But samples from MSDN (Authentication in WinHTTP) checks for this value after WinHttpReceiveResponse.

乍一看 the sample 可能看起来像那样。但是如果你仔细观察,如果 either WinHttpSendRequest() or WinHttpReceiveResponse() 失败,样本实际上会检查 ERROR_WINHTTP_RESEND_REQUEST :

// Send a request.
bResults = WinHttpSendRequest( hRequest,
                               WINHTTP_NO_ADDITIONAL_HEADERS,
                               0,
                               WINHTTP_NO_REQUEST_DATA,
                               0, 
                               0, 
                               0 );

// End the request.
if( bResults )
  bResults = WinHttpReceiveResponse( hRequest, NULL );

// Resend the request in case of 
// ERROR_WINHTTP_RESEND_REQUEST error.
if( !bResults && GetLastError( ) == ERROR_WINHTTP_RESEND_REQUEST)
    continue;

如果 WinHttpSendRequest() returns FALSE,将跳过对 WinHttpReceiveResponse() 的调用并检查 GetLastError() 以查找 ERROR_WINHTTP_RESEND_REQUEST。此代码位于 while 循环内,因此 continue 语句将导致跳过循环的剩余部分,因此将再次调用 WinHttpSendRequest()

结论:样本符合参考文档