You receive an incorrect error when an asynchronous Windows HTTP Services (WinHTTP) method call times out (839872)



The information in this article applies to:

  • Microsoft Windows HTTP Services (WinHTTP) 5.1

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:
  1. Add the following variable declarations to the "(General)" section of your Visual Basic file:
    Dim SentTime, ReceivedTime As Date
  2. 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
  3. 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

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. Use Visual Basic 6.0 to create a Standard EXE project. By default, a form that is named Form1 is created.
  2. Add a reference to the Winhttp.dll library to your project. To do this, follow these steps:
    1. On the Project menu, click References. The References - Project1 dialog box appears.
    2. Click Browse. The Add Reference dialog box appears.
    3. In the Add Reference dialog box, locate and then click the winhttp.dll library.
    4. Click Open, and then click OK.
  3. Add a command button control to the Form1 form.
  4. Right-click Form1, and then click View Code.
  5. Add the following code to the Form1 form.

    Notes
    • In the following code, replace ServerName with the name of your server.
    • In the following code, replace ASPPage.asp with the name of an ASP page that takes significantly more time to load than the receive timeout that you are specifying in the code. In the following code, the receive timeout is specified as 3 seconds.
    Dim WithEvents oReq As WinHttpRequest
    
    Private Sub Command1_Click()
        ' Create a WinHttpRequest object.
        Set oReq = New WinHttpRequest
        ' Open an HTTP connection.
        oReq.Open "GET", "http://ServerName/ASPPage.asp", True
        ' Set the receive timeout to 3 seconds.
        oReq.SetTimeouts 60000, 60000, 60000, 3000
        ' Send an HTTP request to the server.
        oReq.Send
    End Sub
    
    Private Sub oReq_OnError(ByVal ErrorNumber As Long, ByVal ErrorDescription As String)
        ' Display the description of the error.
        MsgBox ErrorDescription
    End Sub
    
    Private Sub oReq_OnResponseFinished()
        ' Display the response from the server.
        MsgBox oReq.ResponseText
    End Sub
  6. On the Run menu, click Start. The Form1 form appears.
  7. In your Web browser, try to open the ASP page that you specified in your code.
  8. Switch to the Form1 form.
  9. Click Command1.
  10. After approximately 3 seconds, the behavior that is mentioned in the "Symptoms" section occurs.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MajorLast Reviewed:5/28/2004
Keywords:kberrmsg kbhttp kbbug KB839872 kbAudDeveloper