如何使用asp设置HTTP超时时间



这是我的asp代码

<%
http = server.createobject("microsoft.xmlhttp")
http.open "post", servleturl, false
http.setrequestheader "content-type", "application/x-www-form-urlencoded"
http.setrequestheader "accept-encoding", "gzip, deflate"
http.send  "request=" & sxml
http_response = http.responsetext
%>

我需要使超时时,响应不来在15秒如何?

您还可以通过调用" settimeout "来继续使用同步请求,如下所示:

<%
Dim http
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.SetTimeouts 600000, 600000, 15000, 15000
http.Open "post", servleturl, false
http.SetRequestHeader "content-type", "application/x-www-form-urlencoded"
http.SetRequestHeader "accept-encoding", "gzip, deflate"
http.Send  "request=" & sxml
http_response = http.responsetext
%>

查看文档。

参数为:

setTimeouts (long resolveTimeout, long connectTimeout, long sendTimeout, long receiveTimeout)

settimeout方法应该在open方法之前调用。参数均为可选参数。

.Send调用之后使用ServerXMLHTTP实例的waitForResponse方法是一种合适的方法,我推荐。
同样要使用.WaitForResponse,需要通过设置.Open方法的第三个参数True来进行异步调用。

Const WAIT_TIMEOUT = 15
Dim http
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
    http.open "POST", servleturl, True 'async request
    http.setrequestheader "content-type", "application/x-www-form-urlencoded"
    http.setrequestheader "accept-encoding", "gzip, deflate"
    http.send  "request=" & sxml
    If http.waitForResponse(WAIT_TIMEOUT) Then 'response ready
        http_response = http.responseText
    Else 'wait timeout exceeded
        'Handling timeout etc
        'http_response = "TIMEOUT" 
    End If
Set http = Nothing

最新更新