Complete Script solution for page Chinese garbled characters

Source: Internet
Author: User

Full Script solution for Chinese garbled characters when using XMLHTTP Post/Get HTML pages

First, let's take a look at how E-text forms are submitted:

The following is a reference clip:
<SCRIPT language = "JavaScript">
StrA = "submit1 = Submit & text1 = scsdfsd ";
Var oReq = new ActiveXObject ("MSXML2.XMLHTTP ");
OReq. open ("POST", "http: // ServerName/VDir/TstResult. asp", false );
OReq. setRequestHeader ("Content-Length", strA. length );
OReq. setRequestHeader ("CONTENT-TYPE", "application/x-www-form-urlencoded ");
OReq. send (strA );
</ScRIPT>



If you replace strA = "submit1 = Submit & text1 = scsdfsd":
StrA = "submit1 = Submit & text1 = Chinese ";

You will find that the submitted stuff is not correct at all, and the Request. Form ("Text1") in ASP cannot get the value at all. I used Request. BinaryRead to write the Post content in an HTML Form and read it out. I found the problem-the Form must be encoded when submitted. The Encoded chinese is similar to % ?? % ?? For example, "Chinese" is encoded as: % D6 % D0 % CE % C4. Well, it's also silly. The CONTENT-TYPE clearly states that application/x-www-form-urlencoded and urlencoded are exactly the same. In this case, we also know what to do-convert ourselves. For the code, see:

The following is a reference clip:
<SCRIPT language = "VBScript">
Function URLEncoding (vstrIn)
StrReturn = ""
For I = 1 To Len (vstrIn)
ThisChr = Mid (vStrIn, I, 1)
If Abs (Asc (ThisChr) <& HFF Then
StrReturn = strReturn & ThisChr
Else
InnerCode = Asc (ThisChr)
If innerCode <0 Then
InnerCode = innerCode + & H10000
End If
Hight8 = (innerCode And & HFF00) \ & HFF
Low8 = innerCode And & HFF
StrReturn = strReturn & "%" & Hex (Hight8) & "%" & Hex (Low8)
End If
Next
URLEncoding = strReturn
End Function

StrA = URLEncoding ("submit1 = Submit & text1 = Chinese ")
OReq = CreateObject ("MSXML2.XMLHTTP ")
OReq. open "POST", "http: // ServerName/VDir/TstResult. asp", false
OReq. setRequestHeader "Content-Length", Len (strA)
OReq. setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
OReq. send strA
</ScRIPT>



(Here, I changed the previous JavaScript code to VBScript, which is not enough to do. For the reason, see later)

Part II. Correctly display the obtained Chinese content

OK. If you write the Form content to the database/file on the Server side, there is no problem with the Chinese text you see there. However, if you want to see the Server's Response -- the problem is: if the Response result is not XML, XMLHTTP. of course there will be no stuff in responseXML, so use responseText. add a sentence at the end of the Code:

Alert (oReq. responseText)
Let's look at the results of our hard work: P

But how can all Chinese characters be changed to squares? (I can't try it out. If I want to try it myself, I don't need to Post it. Get a webpage containing Chinese characters and I will find it .)

The reason is simple: when XMLHTTP gets Response, it is assumed that Response is UTF8 encoded. If Response is XML, you can also specify encoding through encoding, but HTML will not work. (Go to hell !) Therefore, it treats HTML with GB2312 Encoding As UTF8 format, so there is no error!

But there is still a remedy: the undecoded Resonse included in the responseBody attribute of XMLHTTP -- "a raw undecoded bytes as decoded ed directly from the server" :), the only problem is that, responseBody returns an unsigned bytes array. How can we access it and convert it to BSTR?

This is why I changed the code above to VBScript-VBScript Can do it, but JavaScript Cannot!

For the code, see:

The following is a reference clip:
<SCRIPT language = "VBScript">
Function URLEncoding (vstrIn)
StrReturn = ""
For I = 1 To Len (vstrIn)
ThisChr = Mid (vStrIn, I, 1)
If Abs (Asc (ThisChr) <& HFF Then
StrReturn = strReturn & ThisChr
Else
InnerCode = Asc (ThisChr)
If innerCode <0 Then
InnerCode = innerCode + & H10000
End If
Hight8 = (innerCode And & HFF00) \ & HFF
Low8 = innerCode And & HFF
StrReturn = strReturn & "%" & Hex (Hight8) & "%" & Hex (Low8)
End If
Next
URLEncoding = strReturn
End Function

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

StrA = URLEncoding ("submit1 = Submit & text1 = Chinese ")
OReq = CreateObject ("MSXML2.XMLHTTP ")
OReq. open "POST", "http: // ServerName/VDir/TstResult. asp", false
OReq. setRequestHeader "Content-Length", Len (strA)
OReq. setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
OReq. send strA
Alert bytes2BSTR (oReq. responseBody)
</ScRIPT>

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.