Part I Post中文內(nèi)容
先看看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=中文";
你會(huì)發(fā)現(xiàn)提交上去的東東根本不對(duì),ASP中Request.Form("Text1")根本取不到值。俺用Request.BinaryRead把一個(gè)HTML Form中的Post內(nèi)容寫出來看了看,才發(fā)現(xiàn)問題——Form提交時(shí)也要編碼的,編碼后的中文是類似于%??%??的轉(zhuǎn)義字符,比如“中文”就被編碼為:%D6%D0%CE%C4。呵呵,也怪俺笨,人家CONTENT-TYPE里明明寫的清清楚楚——application/x-www-form-urlencoded,urlencoded嘛當(dāng)然就是這個(gè)樣子了。既然這樣,那我們也知道該怎么辦了——自己做轉(zhuǎn)換,代碼見下:
代碼如下:
<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.正確顯示得到的中文內(nèi)容
OK,如果你在Server端把Form的內(nèi)容寫到數(shù)據(jù)庫(kù)/文件的話,你在那里看到的中文毫無問題,但是,假如你想看看Server的Response——問題來了:如果Response的結(jié)果不是XML,XMLHTTP.responseXML里當(dāng)然是不會(huì)有東東的,那就用responseText好了,在代碼的最后加一句:
alert(oReq.responseText)
看看俺們辛勤勞動(dòng)的結(jié)果
但是但是.....怎么所有的中文全變成了方格? (我打不出來,有興趣自己去試,也不用Post,Get一個(gè)含有中文的網(wǎng)頁(yè)就可以發(fā)現(xiàn)了。)
原因很簡(jiǎn)單:XMLHTTP得到Response時(shí)假定Response是UTF8編碼的,如果Response是XML,那還可以通過encoding來指定編碼,但HTML就不行了。(見鬼的GB2312,再次打倒?。┧运押珿B2312編碼的HTML當(dāng)成UTF8格式,不出錯(cuò)才有鬼!
不過好在還有補(bǔ)救的辦法:XMLHTTP的responseBody 屬性里包含的可是未解碼的Resonse——"a raw undecoded bytes as received directly from the server" :),唯一的問題是,responseBody返回的是一個(gè)unsigned bytes數(shù)組,我們?cè)趺慈ピL問它,怎么把它轉(zhuǎn)換成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>
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com