欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP模拟POST请求异步提交数据的方法

程序员文章站 2022-04-15 10:32:03
有时需要获取远程网站的某些信息,而服务器又限制了get方式,只能通过post数据提交,这个时候我们可以通过asp来实现模拟提交post数据,网上有挺多这样的例子的。下面的是...

有时需要获取远程网站的某些信息,而服务器又限制了get方式,只能通过post数据提交,这个时候我们可以通过asp来实现模拟提交post数据,网上有挺多这样的例子的。下面的是我自己写的比较简洁易懂的函数。

首先,需要一个编码设置的函数,因为asp一般为gbk的,而标准的网站现在大都使用utf-8的。所以需要转换。

复制代码 代码如下:

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

其次就是用组件实现post数据的提交了,我这里使用了msxml2.serverxmlhttp.3.0。当然也可以使用其他的。

复制代码 代码如下:

function posthttppage(url,data)
dim http
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

使用的时候就是这样子:

复制代码 代码如下:

posthttppage("www.jb51.net","str1=a&str2=b&str3=c")