Full client script solution for Chinese garbled characters on xmlhttp get html pages

Source: Internet
Author: User
It is often said that they are always worried about Chinese garbled characters when using XMLHTTP. I have read some documents and I am disappointed with the results. All of you are using ASP server technology to solve this problem.

First, let's analyze why Chinese characters are garbled. The reason is simple: when XMLHTTP gets response, it is assumed that response is utf8 encoded. It treats HTML with gb2312 Encoding As utf8 format, so Chinese garbled characters appear.

So, is there no client solution besides the ASP server-side scripting technology? The answer is: yes!

I used the VBScript client script to successfully implement the non-use of ASP, which solved the problem of Chinese garbled characters when XMLHTTP captured HTML pages.

Why does VBScript be used instead of common jscripts? The responsebody of XMLHTTP returns an unsigned bytes array. VBScript provides many functions for operating 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, and lenb to access the responsebody.

I am not emphasizing that VBScript is better than JScript, but that both of them have their own characteristics. I wrote an article on csdn for the first time. Thank you for your support. There are two purposes for writing this article: 1. Exercise yourself; 2. When you encounter problems, learn to analyze the problems and be specific.

I gave the code test.htm, which includes two types of applications: getting your own code and getting other webpage 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> full client script solution for Chinese garbled characters on xmlhttp get html page </TD>
</Tr>
<Tr>
<TD class = button> <button onclick = viewsource1 ()> View Your webpage Code </button> </TD>
</Tr>
<Tr>
<TD class = button> <button onclick = viewsource2 ()> View Google homepage Code </button> </TD>
</Tr>
</Table>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.