如何在ASP中执行HTTP POST请求?

9

我该如何使用经典asp(不是.net)创建带有POST数据的HTTP请求?

2个回答

19

你可以尝试像这样:

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
  • 方法: "GET" 或 "POST"
  • URL:您要发布到的 URL
  • 异步:默认为 False(调用不会立即返回)- 设置为 True 以进行异步调用
  • 用户:需要身份验证的用户名
  • 密码:需要身份验证的密码

当调用返回时,状态属性将包含 HTTP 状态。200 的值表示“OK” - 404 表示未找到,500 表示服务器错误等(请参见 http://en.wikipedia.org/wiki/List_of_HTTP_status_codes 获取其他值)。

您可以获取响应文本(responseText 属性)、XML(responseXML 属性)或流(responseStream 属性)。


5
你需要设置正确的MSXML2.ServerXMLHTTP版本以匹配你所安装的版本。或者只需使用MSXML2.ServerXMLHTTP.3.0,它始终存在于当前受支持的所有平台上。 - AnthonyWJones

0

你必须直接使用现有的XMLHttpRequest服务器对象之一,或者使用一个库来让生活更轻松,将底层细节抽象化。

查看获取URL的Ajax实现

缺点:您需要配置库才能使其正常工作。不确定这是否对您的项目必要。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接