How to perform an HTTP POST request in ASP?

前端 未结 2 420
长情又很酷
长情又很酷 2020-12-01 15:07

How would I go about creating a HTTP request with POST data in classic asp (not .net) ?

相关标签:
2条回答
  • 2020-12-01 15:46

    You can try something like this:

    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
    

    where PostData is the data you want to post (eg name-value pairs, XML document or whatever).

    You'll need to set the correct version of MSXML2.ServerXMLHTTP to match what you have installed.

    The open method takes five arguments, of which only the first two are required:

    ServerXmlHttp.open Method, URL, Async, User, Password
    
    • Method: "GET" or "POST"
    • URL: the URL you want to post to
    • Async: the default is False (the call doesn't return immediately) - set to True for an asynchronous call
    • User: the user name required for authentication
    • Password: the password required for authentication

    When the call returns, the status property holds the HTTP status. A value of 200 means OK - 404 means not found, 500 means server error etc. (See http://en.wikipedia.org/wiki/List_of_HTTP_status_codes for other values.)

    You can get the response as text (responseText property), XML (responseXML property) or a stream (responseStream property).

    0 讨论(0)
  • 2020-12-01 16:02

    You must use one of the existing xmlhttp server objects directly or you could use a library which makes life a bit easier by abstracting the low level stuff away.

    Check ajaxed implementation of fetching an URL

    Disadvantage: You need to configure the library in order to make it work. Not sure if this is necessary for your project.

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