This article mainly introduces the method of ASP simulating POST request to submit data asynchronously. This article uses MSXML2.SERVERXMLHTTP.3.0 to implement POST request. Friends in need can refer to the following
Sometimes it is necessary to obtain certain information from a remote website, and the server restricts the GET method and can only submit it through POST data. At this time, we can simulate submitting post data through asp. There are many such examples on the Internet. The following is a relatively concise and easy-to-understand function that I wrote myself.
First of all, a function for encoding setting is needed, because asp is generally gbk, and most standard websites now use utf-8. So conversion is needed.
Copy the code code as follows:
function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End function
The second step is to use components to submit post data. I used MSXML2.SERVERXMLHTTP.3.0 here. Of course others can also be used.
Copy the code code as follows:
function PostHTTPPage(url,data)
dimHttp
set Http=server.createobject("MSXML2.SERVERXMLHTTP.3.0")
Http.open "POST",url,false
Http.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
Http.send(data)
if Http.readystate<>4 then
exit function
End if
PostHTTPPage=bytesToBSTR(Http.responseBody,"utf-8")
set http=nothing
if err.number<>0 then err.Clear
End function
This is how it works:
Copy the code code as follows:
PostHTTPPage("www.vevb.com","str1=a&str2=b&str3=c")