经典ASP调用API如何设置超时结束?
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
后端 ASP(VBScript + ServerXMLHTTP)设置超时
示例代码vbscript <%
Dim url, apiKey, appId, userContent
url = "http://192.168.0.20/api/"
apiKey = "Bearer xx-..."
appId = "xxxxx"
userContent = "测试内容"
' 构造请求体
Dim requestBody
requestBody = "{""appId"":""" & appId & """,""messages"":[{""role"":""user"",""content"":""" & Replace(userContent, """", "\""") & """}]}"
' 创建 ServerXMLHTTP 对象
Dim http
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
' ---- 设置超时(单位:毫秒) ----
' 参数顺序:resolveTimeout, connectTimeout, sendTimeout, receiveTimeout
' 我们主要设置接收超时为 5000ms(5秒),其他设置为 0 表示默认(无限)
http.setTimeouts 0, 0, 0, 5000
' 发起请求
On Error Resume Next ' 启用错误捕获,以便处理超时异常
http.Open "POST", url, False
http.setRequestHeader "Content-Type", "application/json"
http.setRequestHeader "Authorization", apiKey
http.send requestBody
' 检查是否超时或出错
If Err.Number <> 0 Then
' 超时或其他错误发生
Response.Write "请求超时或失败,执行备选操作"
' 这里可以执行其他逻辑,比如记录日志、返回默认值等
Err.Clear
Else
' 正常处理响应
If http.Status = 200 Then
Response.Write "成功获取:<pre>" & http.responseText & "</pre>"
Else
Response.Write "HTTP 错误:" & http.Status
End If
End If
on error goto 0
Set http = Nothing
%>关键点:
该文章在 2026/9/4 11:17:45 编辑过 |
关键字查询
相关文章
正在查询... |