如何在 ASP 中执行 HTTP POST 请求?
-
13-09-2019 - |
题
我将如何在经典 asp(而不是 .net)中创建带有 POST 数据的 HTTP 请求?
解决方案
你可以尝试这样的事情:
Set ServerXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
ServerXmlHttp.open "POST", "http://www.example.com/page.asp"
ServerXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ServerXmlHttp.setRequestHeader "Content-Length", Len(PostData)
ServerXmlHttp.send PostData
If ServerXmlHttp.status = 200 Then
TextResponse = ServerXmlHttp.responseText
XMLResponse = ServerXmlHttp.responseXML
StreamResponse = ServerXmlHttp.responseStream
Else
' Handle missing response or other errors here
End If
Set ServerXmlHttp = Nothing
其中 PostData 是您要发布的数据(例如名称-值对、XML 文档或其他内容)。
您需要设置正确的 MSXML2.ServerXMLHTTP 版本以匹配您已安装的版本。
open 方法有五个参数,其中只需要前两个:
ServerXmlHttp.open Method, URL, Async, User, Password
- 方法:“获取”或“发布”
- 网址:您要发布到的 URL
- 异步:默认值为 False(调用不会立即返回) - 对于异步调用设置为 True
- 用户:身份验证所需的用户名
- 密码:身份验证所需的密码
当调用返回时,status 属性保存 HTTP 状态。值 200 表示正常 - 404 表示未找到,500 表示服务器错误等。(看 http://en.wikipedia.org/wiki/List_of_HTTP_status_codes 对于其他值。)
您可以获取文本(responseText 属性)、XML(responseXML 属性)或流(responseStream 属性)形式的响应。
其他提示
您必须使用现有的XMLHTTP服务器的一个对象直接或者你可以使用一个库,让生活更容易一点通过提取低层次的东西了。
检查获取的URL的 ajaxed实施
缺点:您需要配置的存储库,以使其发挥作用。不知道这是必要为您的项目。
不隶属于 StackOverflow