How do I POST data to a remote URL in Classic ASP?

后端 未结 8 781
走了就别回头了
走了就别回头了 2020-12-08 08:06

I need to POST data to a url in the middle of a script.

  1. User fills out form:
  2. Form submits to process.asp: I need to PO
相关标签:
8条回答
  • 2020-12-08 08:46

    Most form action pages accept data as a 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
    

    When creating the data url encoding is needed on the data values. Since ASPs Server.URLEncode method only does path encoding and not component encoding you need to replace out / characters with %2F

    Function URLEncodeComponent(value)
        URLEncodeComponent = Server.URLEncode(value)
        URLEncodeComponent = Replace(URLEncodeComponent, "/", "%2F")
    End Function
    
    0 讨论(0)
  • 2020-12-08 08:47

    OK all the answers were very complicated and I know you already selected a solution - but I feel like the simple Server.Transfer() command could have done exactly what you need.

    At the end of your script instead of Response.Redirect(url) to the new page - just do a Server.Transfer(url) and it will pass your entire Request collection across to the next page.

    Read about it here (support.microsoft.com).

    There are some catches (i.e. it keeps the same URL on the browser so it can play tricks with the back button and such) but otherwise it's pretty simple.

    0 讨论(0)
提交回复
热议问题