Full client script solution for Chinese garbled XMLHTTP gethtml page

Source: Internet
Author: User
Tags chr end functions html page client
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>
&LT;TD class=text>xmlhttp get HTML page with Chinese garbled full client script solution </td>
</tr>
<tr>
&LT;TD Class=button><button Onclick=viewsource1 () > View its own page code </button></td>
</tr>
<tr>
&LT;TD Class=button><button Onclick=viewsource2 () > View Google homepage code </button></td>
</tr>
</TABLE>
</BODY>
</HTML>



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.