Part I post Chinese content
First, let's take a look at how e-text forms are submitted:
CopyCode The Code is as follows: <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 change stra = "submit1 = submit & text1 = scsdfsd"; to: 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: Copy code The Code is as follows: 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. At the end of the Code, add: Alert (oreq. responsetext ). Let's look at the results of our hard work.
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: Copy code The Code is as follows: <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)