这是我的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
%>
我需要制作TimeOut,当响应在15秒内没有响应时如何?
答案 0 :(得分:14)
您还可以通过调用“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
%>
请参阅此处查看docs。
参数是:
setTimeouts (long resolveTimeout, long connectTimeout, long sendTimeout, long receiveTimeout)
应该在open方法之前调用setTimeouts方法。这些参数都不是可选的。
答案 1 :(得分:8)
在ServerXMLHTTP
通话后使用.Send
实例的waitForResponse方法是正确的方法,我建议。
另外要使用.WaitForResponse
,需要通过设置True
.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