8

这是我的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 秒内没有响应时进行 TimeOut 怎么办?

4

2 回答 2

18

您还可以通过像这样调用“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
%>

有关文档,请参见此处。

参数是:

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

setTimeouts 方法应该在 open 方法之前调用。没有一个参数是可选的。

于 2012-12-27T14:36:56.593 回答
10

我建议在调用后使用实例的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
于 2012-12-27T14:00:27.300 回答