摘要:xmlhttp 應用
一般寫法如下:
01
<%
02
Function getHTTPPage(url)
03
dim objXML
04
set objXML=server.createobject("MSXML2.XMLHTTP")'定義
05
objXML.open "GET",url,false'打開
06
objXML.send()'發送
07
If objXML.readystate<>4 then '判斷文檔是否已經解析完,以做用戶端接受返回消息
08
exit function
09
End If
10
getHTTPPage=BytesToBstr(objXML.responseBody)'返回資訊,同時用函數定義編碼
11
set objXML=nothing'關閉
12
if err.number<>0 then err.Clear
13
End Function
14
15
Function BytesToBstr(body)
16
dim objstream
17
set objstream = Server.CreateObject("adodb.stream")
18
objstream.Type = 1
19
objstream.Mode =3
20
objstream.Open
21
objstream.Write body
22
objstream.Position = 0
23
objstream.Type = 2
24
objstream.Charset = "BIG5"
25
'轉換原來默認的UTF-8編碼轉換成BIG5編碼,否則直接用XMLHTTP調用有中文字元的網頁得到的將是亂碼
26
BytesToBstr = objstream.ReadText
27
objstream.Close
28
set objstream = nothing
29
End Function
30
31
Dim Url,Html
32
Url = "http://www.cnbruce.com/blog"
33
Html = getHTTPPage(Url)
34
response.write(Html)
35
%>
<% 02
Function getHTTPPage(url) 03
dim objXML 04
set objXML=server.createobject("MSXML2.XMLHTTP")'定義 05
objXML.open "GET",url,false'打開 06
objXML.send()'發送 07
If objXML.readystate<>4 then '判斷文檔是否已經解析完,以做用戶端接受返回消息 08
exit function 09
End If 10
getHTTPPage=BytesToBstr(objXML.responseBody)'返回資訊,同時用函數定義編碼 11
set objXML=nothing'關閉 12
if err.number<>0 then err.Clear 13
End Function 14
15
Function BytesToBstr(body) 16
dim objstream 17
set objstream = Server.CreateObject("adodb.stream") 18
objstream.Type = 1 19
objstream.Mode =3 20
objstream.Open 21
objstream.Write body 22
objstream.Position = 0 23
objstream.Type = 2 24
objstream.Charset = "BIG5" 25
'轉換原來默認的UTF-8編碼轉換成BIG5編碼,否則直接用XMLHTTP調用有中文字元的網頁得到的將是亂碼 26
BytesToBstr = objstream.ReadText 27
objstream.Close 28
set objstream = nothing 29
End Function 30
31
Dim Url,Html 32
Url = "http://www.cnbruce.com/blog" 33
Html = getHTTPPage(Url) 34
response.write(Html) 35
%>
可再加上一行:
getHTTPPage=bytes2BSTR(objXML.responseBody)'或者返回信息時用函數轉換漢字
01
Function bytes2BSTR(vIn)
02
strReturn = ""
03
For j = 1 To LenB(vIn)
04
ThisCharCode = AscB(MidB(vIn,j,1))
05
If ThisCharCode < &H80 Then
06
strReturn = strReturn & Chr(ThisCharCode)
07
Else
08
NextCharCode = AscB(MidB(vIn,j+1,1))
09
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
10
j = j + 1
11
End If
12
Next
13
bytes2BSTR = strReturn
14
End Function
Function bytes2BSTR(vIn) 02
strReturn = "" 03
For j = 1 To LenB(vIn) 04
ThisCharCode = AscB(MidB(vIn,j,1)) 05
If ThisCharCode < &H80 Then 06
strReturn = strReturn & Chr(ThisCharCode) 07
Else 08
NextCharCode = AscB(MidB(vIn,j+1,1)) 09
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) 10
j = j + 1 11
End If 12
Next 13
bytes2BSTR = strReturn 14
End Function
lenB返回字節數而不是字符數,同理ascB返回每個字節的ascii碼,大于80h,也就是128的ascii是漢字——半個漢字,把半個半個的漢字ascii碼拼合再用chr函數返回字符就可以了。
Function
Function