如何在Classic ASP中将数据发布到远程URL?

nrh*_*ond 20 asp-classic

我需要POST在脚本中间的数据到URL.

  1. 用户填写表格:
  2. 表单提交到process.asp:此时我需要将POST数据发送到第三方集成.
  3. process.asp 完成并指导用户感谢您的页面.

Joh*_*ose 31

我不确定为什么每个人都在发布ASP.Net解决方案,当你明确表示你正在使用ASP"经典"时.

这样的事情应该有效.我没有写代码; 我在别处找到了它.但是,如果您不想购买商业广告,则可以使用MSXML2.ServerXMLHTTP对象.

function getHTML (strUrl)
    Set xmlHttp = Server.Createobject("MSXML2.ServerXMLHTTP")
    xmlHttp.Open "GET", strUrl, False
    xmlHttp.setRequestHeader "User-Agent", "asp httprequest"
    xmlHttp.setRequestHeader "content-type", "application/x-www-form-urlencoded"
    xmlHttp.Send
    getHTML = xmlHttp.responseText
    xmlHttp.abort()
    set xmlHttp = Nothing   
end function 
Run Code Online (Sandbox Code Playgroud)

您可能需要添加一些错误处理代码,以便在生产环境中使用.我相信如果遇到404或超时错误,该对象会抛出错误.您需要通过在.Send之前设置On Error Resume Next来"捕获"它们的ASP样式(yuck),然后检查ASP错误对象以查看是否存在问题.

祝好运!

  • UserAgent并不总是多余的 - 根据它,您实际上可能会在响应中获得不同的内容. (3认同)

Ant*_*nes 5

大多数表单操作页面都接受数据作为POST.

Function postFormData(url, data)
    Dim xhr : Set xhr = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    xhr.open "POST", url, false
    xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    xhr.send Data
    If xhr.Status = 200 Then
       postFormData = xhr.ResponseText
    Else
        Err.Raise 1001, "postFormData", "Post to " & url & " failed with " & xhr.Status
    End If
End Function
Run Code Online (Sandbox Code Playgroud)

在创建数据时,需要对数据值进行编码.由于ASPs Server.URLEncode方法只进行路径编码而不进行组件编码,因此需要用%2F替换/字符

Function URLEncodeComponent(value)
    URLEncodeComponent = Server.URLEncode(value)
    URLEncodeComponent = Replace(URLEncodeComponent, "/", "%2F")
End Function
Run Code Online (Sandbox Code Playgroud)