ASP模擬POST請(qǐng)求異步提交數(shù)據(jù)的方法

字號(hào):


    有時(shí)需要獲取遠(yuǎn)程網(wǎng)站的某些信息,而服務(wù)器又限制了GET方式,只能通過(guò)POST數(shù)據(jù)提交,這個(gè)時(shí)候我們可以通過(guò)asp來(lái)實(shí)現(xiàn)模擬提交post數(shù)據(jù),網(wǎng)上有挺多這樣的例子的。下面的是我自己寫(xiě)的比較簡(jiǎn)潔易懂的函數(shù)。
    首先,需要一個(gè)編碼設(shè)置的函數(shù),因?yàn)閍sp一般為gbk的,而標(biāo)準(zhǔn)的網(wǎng)站現(xiàn)在大都使用utf-8的。所以需要轉(zhuǎn)換。
    代碼如下:
    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
    其次就是用組件實(shí)現(xiàn)post數(shù)據(jù)的提交了,我這里使用了MSXML2.SERVERXMLHTTP.3.0。當(dāng)然也可以使用其他的。
    代碼如下:
    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
    使用的時(shí)候就是這樣子:
    代碼如下: