用XMLHTTP Post Form時的表單亂碼有兩方面的原因——Post表單數據時中文亂碼;服務器Response被XMLHTTP不正確編碼引起的亂碼。換句話說,本文主要解決兩個問題——怎樣正確Post中文內容&怎樣正確顯示得到的中文內容。
Part I Post中文內容
先看看E文的表單是怎麼提交的:
復制代碼 代碼如下:
<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>
如果把strA = "submit1=Submit&text1=scsdfsd";換成:
strA = "submit1=Submit&text1=中文";
你會發現提交上去的東東根本不對,ASP中Request.Form("Text1")根本取不到值。俺用Request.BinaryRead把一個HTML Form中的Post內容寫出來看了看,才發現問題——Form提交時也要編碼的,編碼後的中文是類似於%??%??的轉義字符,比如“中文”就被編碼為:%D6%D0%CE%C4。呵呵,也怪俺笨,人家CONTENT-TYPE裡明明寫的清清楚楚——application/x-www-form-urlencoded,urlencoded嘛當然就是這個樣子了。既然這樣,那我們也知道該怎麼辦了——自己做轉換,代碼見下:
復制代碼 代碼如下:
<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=中文")
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>
(在這裡俺把前面的JavaScript的代碼改成了VBScript,不是吃飽了撐的沒事干,原因見後)
Part II.正確顯示得到的中文內容
OK,如果你在Server端把Form的內容寫到數據庫/文件的話,你在那裡看到的中文毫無問題,但是,假如你想看看Server的Response——問題來了:如果Response的結果不是XML,XMLHTTP.responseXML裡當然是不會有東東的,那就用responseText好了,在代碼的最後加一句:
alert(oReq.responseText)
看看俺們辛勤勞動的結果
但是但是.....怎麼所有的中文全變成了方格? (我打不出來,有興趣自己去試,也不用Post,Get一個含有中文的網頁就可以發現了。)
原因很簡單:XMLHTTP得到Response時假定Response是UTF8編碼的,如果Response是XML,那還可以通過encoding來指定編碼,但HTML就不行了。(見鬼的GB2312,再次打倒!)所以它把含GB2312編碼的HTML當成UTF8格式,不出錯才有鬼!
不過好在還有補救的辦法:XMLHTTP的responseBody 屬性裡包含的可是未解碼的Resonse——"a raw undecoded bytes as received directly from the server" :),唯一的問題是,responseBody返回的是一個unsigned bytes數組,我們怎麼去訪問它,怎麼把它轉換成BSTR?
這就是為什麼我在上面把代碼改成VBScript的原因——VBScript Can do it,but JavaScript Cannot!
代碼見下:
復制代碼 代碼如下:
<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=中文")
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)
</SSRIPT>