How to perform an HTTP POST request in ASP?

2019-01-09 02:05发布

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

2条回答
劳资没心,怎么记你
2楼-- · 2019-01-09 02:27

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-09 02:31

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).

查看更多
登录 后发表回答