xml| Solution | client | page | chinese | Chinese garbled
It is often said that they use the XMLHTTP process, always for the Chinese garbled problem trouble. I looked up some information, and I was disappointed that we were using ASP server-side technology to solve the problem.
First of all, analyze why there is a problem of garbled Chinese. The reason is very simple: XMLHTTP get response assumed that response is UTF8 encoded, it contains GB2312 encoded HTML as a UTF8 format, therefore, the emergence of Chinese garbled.
So, in addition to using ASP server-side scripting technology, there is no client solution? The answer is: Yes!
I use VBScript client script, successfully implemented without using ASP, solve the XMLHTTP crawl HTML page when the problem of Chinese garbled.
Why use VBScript instead of the common JScript? XMLHTTP's responsebody returns an array of unsigned bytes. VBScript provides a number of functions for manipulating strings and formatting data, as well as methods for accessing secure arrays. These functions or methods do not exist in JScript. Here we need to use the built-in functions of VBScript: MidB, AscB, LENB, etc. to access responsebody.
By the way, I'm not stressing that VBScript is better than JScript, but that both have their own characteristics. The first time in CSDN write articles, thank you for your support. Write this article there are two purposes: first, to exercise their own; I hope you encounter problems, we should learn to analyze the problem, to be targeted, know it and then know why.
I give the code test.htm, it includes two applications to get their own code and get other web code, the specific script is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<!--Author: Kobayashi, sulins@tom.com-->
<HTML>
<HEAD>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</HEAD>
<script language=vbscript>
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.XMLHTTP")
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.XMLHTTP")
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>xmlhttp get HTML page with Chinese garbled full client script solution </td>
</tr>
<tr>
<TD Class=button><button Onclick=viewsource1 () > View its own page code </button></td>
</tr>
<tr>
<TD Class=button><button Onclick=viewsource2 () > View Google homepage code </button></td>
</tr>
</TABLE>
</BODY>
</HTML>