Microsoft KB Archive/839872

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

Article ID: 839872

Article Last Modified on 3/20/2007

-

APPLIES TO


 * Microsoft Windows HTTP Services 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  with the name of your server, and replace   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 &quot;GET&quot;, &quot;http://ServerName/ASPPage.asp&quot;, 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 &quot;(General)&quot; 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(&quot;s&quot;, 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 &quot;The operation timed out.&quot; Else ' Display the description of the error that has occurred. MsgBox ErrorDescription End If End If </li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.

<div class="moreinformation_section">

Steps to reproduce the behavior
<ol> Use Visual Basic 6.0 to create a Standard EXE project. By default, a form that is named Form1 is created.</li> Add a reference to the Winhttp.dll library to your project. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the Project menu, click References. The References - Project1 dialog box appears.</li> Click Browse. The Add Reference dialog box appears.</li> In the Add Reference dialog box, locate and then click the winhttp.dll library.</li> Click Open, and then click OK.</li></ol> </li> Add a command button control to the Form1 form.</li> Right-click Form1, and then click View Code.</li>  Add the following code to the Form1 form.

Notes  In the following code, replace  with the name of your server.</li> In the following code, replace  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.</li></ul>

Dim WithEvents oReq As WinHttpRequest

Private Sub Command1_Click ' Create a WinHttpRequest object. Set oReq = New WinHttpRequest ' Open an HTTP connection. oReq.Open &quot;GET&quot;, &quot;http://ServerName/ASPPage.asp&quot;, 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 </li> On the Run menu, click Start. The Form1 form appears.</li> In your Web browser, try to open the ASP page that you specified in your code.</li> Switch to the Form1 form.</li> Click Command1.</li> <li>After approximately 3 seconds, the behavior that is mentioned in the &quot;Symptoms&quot; section occurs.</li></ol>

<div class="references_section">