如何使用asp设置http超时?

use*_*464 7 xml asp-classic

这是我的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
%>
Run Code Online (Sandbox Code Playgroud)

我需要制作TimeOut,当响应没有在15秒内到来时如何?

gpi*_*kas 15

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

<%
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
%>
Run Code Online (Sandbox Code Playgroud)

请参阅此处了解文档.

参数是:

setTimeouts (long resolveTimeout, long connectTimeout, long sendTimeout, long receiveTimeout)
Run Code Online (Sandbox Code Playgroud)

应该在open方法之前调用setTimeouts方法.这些参数都不是可选的.


Kul*_*gin 9

在调用之后使用实例的waitForResponse方法是正确的方法,我建议. 另外要使用,需要通过设置方法的第三个参数来进行异步调用.ServerXMLHTTP.Send
.WaitForResponseTrue.Open

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
Run Code Online (Sandbox Code Playgroud)