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

后端 未结 8 784
走了就别回头了
走了就别回头了 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
    

提交回复
热议问题