There are two reasons for the form when using XMLHTTP Post Form - the Chinese garbled when the POST form data; the server Response is incorrectly encoded correctly. In other words, this article mainly solves two problems - how to correct post Chinese content & how to correctly display the meaning of the meaning.
Part I POST Chinese Content
Let's take a look at how the E text is submitted:
Stra = "submit1 = submit & text1 = SCSDFSD";
Var oreq = new activXObject ("msxml2.xmlhtp");
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 str = "submit1 = submit & text1 = SCSDFSD";
Stra = "Submit1 = Submit & Text1 = Chinese";
You will find that the Subject of the Submitted Subject is not right, and the request.form ("text1") is not valued at all.俺 Use Request.BinaryRead to write out the POST content in an HTML FORM, I found that the problem - form should also be encoded when I submit, and the encoded Chinese is similar to the escape character of the% ??%, For example, "Chinese" is encoded as:% D6% D0% CE% C4. Oh, it is stupid. The clear clear-in-Type clearly written clearly --Application / x-www-form-urlencoded, urlencoded is of course this. In this case, then we also know what to do - to do it, see below:
Function Urlencoding (vstrin)
Strreturn = ""
For i = 1 to len (vstrin)
Thischr = MID (vstrin, i, 1)
IF ABS (ASCHR) <& HFF THEN
Strreturn = Strreturn & thischr
Else
Innercode = ASC (thischr)
IF innercode <0 THEN
Innercode = InnerCode & H10000
END IF
High8 = (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>
(Change the code of JavaScript in front here to VBScript, not to eat, the reason is seen)
PART II. Correctly displayed the Chinese content
OK, if you write Form's content on the server, you have no problem, but if you want to see the Server's response - the problem: If the result of Response is not XML, XMLHTTP.Responsexml will not have Dongdong, then use responsetext, in the final plus of the code:
Alert (Oreq.ResponseText)
Look at the results of our hard work: P
But ..... how all Chinese has become a square? (I can't play it, I have interested myself to try, nor, don't have pos, get a web page with Chinese.)
The reason is very simple: XMLHTTP gets response to assume that response is UTF8 encoding. If Response is XML, it can also specify the encoding via eNCoding, but HTML is not. (See the ghost GB2312, knock down again!), So it uses the GB2312 encoded HTML as a UTF8 format, and there is no mistake!
But good in the rescue method: XMLHTTP's responsebody property contains unresolved resonse - "a Raw undecoded bytes as receiving directly from the server" :), the only problem is that responsebody returns a unsigned Bytes Array, how do we access it, how to convert it into BSTR?
That's why I changed the code to VBScript. - Vbscript Can do IT, But JavaScript Cannot!
The code is shown in:
Function Urlencoding (vstrin)
Strreturn = ""
For i = 1 to len (vstrin)
Thischr = MID (vstrin, i, 1)
IF ABS (ASCHR) <& HFF THEN
Strreturn = Strreturn & thischr
Else
Innercode = ASC (thischr)
IF innercode <0 THEN
Innercode = InnerCode & H10000
End ifhight8 = (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 (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>