为ServerXMLHTTP对象的HTTP请求设置超时时间

类别:VB语言 点击:0 评论:0 推荐:

ServerXMLHTTP对象的HTTP请求设置超时时间

Article last modified on 2002-3-2

----------------------------------------------------------------

The information in this article applies to:

  - Microsoft XML 3.0

----------------------------------------------------------------

如果用ServerXMLHTTP(SXH)对象向另一个ASP页面或者Internet资源发起HTTP请求,建议设置一下超时限制。

既可以异步提交这种HTTP请求,也可以通过同步操作时调用setTimeouts方法做到这一点。

setTimeouts的各项参数为:

HRESULT setTimeouts (long resolveTimeout, long connectTimeout,

long sendTimeout, long receiveTimeout)

这些参数分别指定了解析DNS名字、建立socket连接、发送Request Data和接收Response Data的超时时间。

    如果不指定超时,那么假如客户端同步发送数据给服务器,而服务器却挂起了这个请求,这样,要么客户端等待5分钟后发生send超时,要么等待一小时后发生receive超时。在这段时间内,客户端表现为Hang。

经常会被问的一个问题是,我如何把这种超时失败同其他错误区分开来?

是这个样子,如果有一个时间超时的错误发生,就会引发一个COM异常:

0x80072ee2 "The operation timed out"

 

附录A中给出一个简单的VB DEMO。如果服务器的RS.asp中执行一句话:Sleep(15000),肯定会send超时,于是异常就被捕获到了。这时的对话框如下所示:

-2147012894就是0x80072EE2的错误。

 

SXH的open方法其实并不作任何network I/O,它只是初始化一下SXH对象的一些内部状态。所有“Real”的工作是在send方法中作的(如果这次的请求是同步的),setTimeouts中设置的超时值也将应用在这里。

send方法解析DNS名字,同服务器建立连接,发送request,等待reponse和读response data入内存。每个超时可能会被应用好多次。比如,如果response较大,SXH将需要调用多次Winsock receive来load response,这时receive超时将被应用到每一个Winsock receive调用上。

附录A:

   #If Win32 Then

      Declare Function GetTickCount Lib "kernel32" () As Long

   #Else

      Declare Function GetTickCount Lib "User" () As Long

   #End If

 

Private Sub Command1_Click()

    On Error Resume Next

    Dim objClient As MSXML2.ServerXMLHTTP

    Dim lngStartTime As Long

    Dim lngFinnishTime As Long

    Dim lngOpenTime As Long

    Dim lngSendTime As Long

    Dim lngRespTime As Long

    Dim strMessage As String

   

    Set objClient = New MSXML2.ServerXMLHTTP

   

    txtOpen.Text = ""

    txtSend.Text = ""

    txtResponse.Text = ""

 

    objClient.setTimeouts 650, 650, 650, 650

 

    lngStartTime = GetTickCount()

   objClient.open "GET", "http://localhost:80/asp/rs.asp", False

   

    ' no failure here - the operation succeeds in 0 ms

    lngFinnishTime = GetTickCount()

    lngOpenTime = lngFinnishTime - lngStartTime

    strMessage = CStr(lngOpenTime)

   

    If Err.Number <> 0 Then

        strMessage = strMessage & ": " & Err.Description

        Err.Clear

    End If

   

    txtOpen.Text = strMessage

    lngStartTime = GetTickCount()

   

    objClient.send ' this is the operation that times out.

   

    lngFinnishTime = GetTickCount()

    lngSendTime = lngFinnishTime - lngStartTime

    strMessage = CStr(lngSendTime)

   

    If Err.Number <> 0 Then

        strMessage = strMessage & ":ErrNumber-" & _

                     Err.Number & "|ErrDescription-" & Err.Description

        Err.Clear

    End If

   

    txtSend.Text = strMessage

    ' this value is set to 672ms appended to a timeout error message

    ' example: "672: The operation timed out"

   

   

    Dim respDom As MSXML2.DOMDocument

    lngStartTime = GetTickCount()

   

    Set respDom = objClient.responseXML

   ' no failure here - the operation succeeds in 0 ms

    lngFinnishTime = GetTickCount()

    lngRespTime = lngFinnishTime - lngStartTime

    strMessage = CStr(lngRespTime)

    If Err.Number <> 0 Then

        strMessage = strMessage & ": " & Err.Description

        Err.Clear

    End If

    txtResponse.Text = strMessage

    Set objClient = Nothing

    Exit Sub

ERR_HANDLER:

    MsgBox Err.Description

 

End Sub

附录B:

下面我们给出常见的SXH对象的错误码及其解释:

0x80004005  Unspecified error

0x8000FFFF  Unexpected error

0x80070008  Out of memory

0x8007000E  Out of memory

0x80072ee1  Cannot create any more ServerXMLHTTP objects (cannot exceed 5460 concurrent objects per process)

0x80072ee2  The operation timed out

0x80072ee4  internal error

0x80072ee5  The URL is invalid

0x80072ee7  The server name or address could not be resolved

0x80072eee  incorrect password

0x80072eef  authentication failure (credentials not accepted)

0x80072efd  A connection with the server could not be established

0x80072efe  connection aborted

0x80072eff  connection reset

0x80072f05  The date in the certificate is invalid or has expired

0x80072f06  The host name in the certificate is invalid or does not match

0x80072f07  A redirect request will change a non-secure to a secure connection

0x80072f08  A redirect request will change a secure to a non-secure connection

0x80072f0c  A certificate is required to complete client authentication

0x80072f0d  The certificate authority is invalid or incorrect

0x80072f0e  Client authentication has not been correctly installed

0x80072f76  The requested header was not found

0x80072f78  The server returned an invalid or unrecognized response

0x80072f7a  The request for a HTTP header is invalid

0x80072f7c  The HTTP redirect request failed

0x80072f7d  An error occurred in the secure channel support

0x80072f80  The HTTP request was not redirected

0x80072f82  A cookie from the server has been declined acceptance

0x80072f88  The HTTP redirect request must be confirmed by the user

0x80072f89  The server certificate was invalid

 

本文地址:http://com.8s8s.com/it/it6390.htm