<% Function getHTTPPage(url) dim objXML set objXML=server.createobject("MSXML2.XMLHTTP")'定义 objXML.open "GET",url,false'打开 objXML.send()'发送 If objXML.readystate<>4 then '判断文档是否已经解析完,以做客户端接受返回消息 exit function End If getHTTPPage=BytesToBstr(objXML.responseBody)'返回信息,同时用函数定义编码 'getHTTPPage=bytes2BSTR(objXML.responseBody)'或者返回信息时用函数转换汉字 set objXML=nothing'关闭 if err.number<>0 then err.Clear End Function %>
接着就来看看定义编码函数BytesToBstr()的主要内容
<% Function BytesToBstr(body) 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 = "GB2312" '转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码 BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function %>
当然,还可以使用专门的函数来处理汉字:
Function bytes2BSTR(vIn) strReturn = "" For j = 1 To LenB(vIn) ThisCharCode = AscB(MidB(vIn,j,1)) If ThisCharCode < &H80 Then strReturn = strReturn & Chr(ThisCharCode) Else NextCharCode = AscB(MidB(vIn,j+1,1)) strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) j = j + 1 End If Next bytes2BSTR = strReturn End Function