常有人说,他们在使用XML 先分析一下为何会出现中文乱码的问题。原因很简单:XML 那么,除了使用ASP服务器端脚本技术外,就没有客户端的解决办法吗?答案是:有!
本人使用VBHTTP 抓取 HTML页面时出现中文乱码的问题。
为何使用VBHTTP的responseBody返回的是一个unsigned bytes数组。VBScript的内置函数:MidB、AscB、LenB等,来访问responseBody。
说句题外话,我不是在强调VB 我给出代码Test.htm,它包括了获取自身代码和获取其他网页代码两种应用,具体脚本如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- 作者:小林,sulins@tom.com -->
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<script language=VB
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
Function viewSource1()
dim XmlHttp
set XmlHttp = CreateObject("Microsoft.XML XmlHttp.Open "GET", document.location.href, false
XmlHttp.setRequestHeader "Content-Type","text/XML XmlHttp.Send
dim html
html = bytes2BSTR(XmlHttp.responseBody)
msgbox html
End Function
Function viewSource2()
dim XmlHttp
set XmlHttp = CreateObject("Microsoft.XML XmlHttp.Open "GET", "http://www.google.com", false
XmlHttp.setRequestHeader "Content-Type","text/XML XmlHttp.Send
dim html
html = bytes2BSTR(XmlHttp.responseBody)
msgbox html
End Function
</script>
<BODY bgcolor=gainsboro style='border:1pt solid white'>
<TABLE class=text>
<tr>
<td class=text>XML </tr>
<tr>
<td class=button><button onclick=viewSource1()>查看自身的网页代码</button></td>
</tr>
<tr>
<td class=button><button onclick=viewSource2()>查看google主页代码</button></td>
</tr>
</TABLE>
</BODY>
</HTML>


