知识库

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

来源: 本站    类别: 知识库    日期: 2015/5/2

 

有时需要获取远程网站的某些信息,而服务器又限制了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.cnhww.com,"str1=a&str2=b&str3=c")

相关文章


Copyright © 2004 - 2024 CNHWW Inc. All Rights Reserved
石家庄市征红网络科技有限公司版权所有 邮政编码:050051
服务电话:0311-85315152 13931185013 在线客服QQ:81447932 / 81447933 邮箱: cnhww@163.com