SYMPTOMS
When an asynchronous Microsoft Windows HTTP Services
(WinHTTP) method call times out, you receive one of the following incorrect
errors:
ERROR_WINHTTP_OPERATION_CANCELLED
ERROR_WINHTTP_INVALID_SERVER_RESPONSE
For
example, in a Microsoft Visual Basic 6.0 application, when an asynchronous
WinHTTP method call times out and you try to display the error description, you
receive either of the following incorrect error messages:
The
operation has been canceled
The server returned an invalid or unrecognized
response
Notes- You should receive the following correct error when an
asynchronous WinHTTP method call times out:
ERROR_WINHTTP_TIMEOUT
- In a Visual Basic 6.0 application, you should receive the following correct error
message when an asynchronous WinHTTP method call
times out:
The operation timed
out
- The socket operation does not have to time out for you to receive the ERROR_WINHTTP_INVALID_SERVER_RESPONSE error message. Instead, you will receive this error when a Web server FINs the connection before it sends back all the headers. There is no workaround for this condition.
WORKAROUND
To work around this behavior, use either of the
following methods.
Use synchronous WinHTTP method calls
Use synchronous WinHTTP method calls instead of asynchronous
WinHTTP method calls. To use synchronous WinHTTP method calls, you must open
the HTTP connection that you want to associate with your
WinHttpRequest object
in the synchronous mode. To do this, pass
False as the value of the
varAsync parameter in the call to the
WinHttpRequest.Open method. For
example, use the following sample code to use a
WinHttpRequest object
to open an HTTP connection in the synchronous mode.
Note In the following code, replace
ServerName with the name of your server, and replace
ASPPage.asp with the name of a Microsoft Active
Server Pages (ASP) page.
' Create a WinHttpRequest object.
Set oReq = New WinHttpRequest
' Open an HTTP connection.
oReq.Open "GET", "http://ServerName/ASPPage.asp", False
Handle incorrect errors in asynchronous WinHTTP method calls
If you receive an incorrect error message when you use asynchronous WinHTTP
method calls, handle the error, and then verify that the time period between the
method call and the occurrence of the error corresponds to the receive timeout
that you have set. If the time period corresponds to the receive timeout that
you have set, the most likely cause for the error is a request timeout. To do
this, follow these steps:
- Add the following variable declarations to the "(General)"
section of your Visual Basic file:
Dim SentTime, ReceivedTime As Date
- Store the time that you make your method call. To do this, add
the following code immediately before the code that you use to make the method
call:
' Store the time that you make the method call.
SentTime = Now
- To handle incorrect errors, you can add code that is
similar to the following code in your event handler:
' Verify that the error number corresponds to either of the possibly incorrect errors.
If ErrorNumber = -2147012879 Or ErrorNumber = -2147012744 Then
' Store the time that the error occurred.
ReceivedTime = Now
' Calculate the time period (in seconds) between the method call and the
' occurrence of the error.
Dim TimePeriod As Long
TimePeriod = DateDiff("s", SentTime, ReceivedTime)
' Verify that the time period corresponds to the receive timeout that you have set.
If TimePeriod = 3 Or TimePeriod = 4 Then
' Display the correct error message.
MsgBox "The operation timed out."
Else
' Display the description of the error that has occurred.
MsgBox ErrorDescription
End If
End If