在UTF-8页中接收和使用以GB2312方式进行URL编码的中文数据

  本文记录我如何在UTF-8页中接收和使用以GB2312方式进行URL编码的中文数据的实现。

  比如“汉”字:
  以GB2312编码进行URLEncode后:%BA%BA
  以UTF-8编码进行URLEncode后:%E6%B1%89

  如果将以GB2312编码进行URLEncode后的数据%BA%BA直接通过URL传递给UTF-8编码的网页,并在网页中直接以Request.QueryString方式读取数据,就会有乱码问题。

  解决方法如下:

功能代码
'说明:在UTF-8编码网页中读取以GB2312方式编码的URLEncode数据
'
使用:sTest = GBQueryString("Test")
Function GBQueryString(ByVal sQueryName)
Dim iCodePage, sQueryValue

iCodePage
= Session.CodePage
Session.CodePage
= 936
sQueryValue
= Request.QueryString(sQueryName)
Session.CodePage
= iCodePage

GBQueryString
= URLDecode(GBToUTF8(sQueryValue))
End Function

Function GBToUTF8(sInput)
Dim wChar, uChar, nAsc, sResult, i

'如果输入参数为空,则退出函数
If sInput = "" Then
GBToUTF8
= sInput
Exit Function
End If
'开始转换
For i = 1 To Len(sInput)
'利用mid函数分拆GB编码文字
wChar = Mid(sInput, i, 1)
'利用ascW函数返回每一个GB编码文字的Unicode字符代码
'注:asc函数返回的是ANSI 字符代码,注意区别
nAsc = AscW(wChar)
If nAsc < 0 Then nAsc = nAsc + 65536

If (nAsc And &HFF80) = 0 Then
sResult
= sResult & wChar
Else
If (nAsc And &HF000) = 0 Then
uChar
= "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & _
Hex(nAsc And &H3F Or &H80)
sResult
= sResult & uChar
Else
'GB编码文字的Unicode字符代码在0800 - FFFF之间采用三字节模版
uChar = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & _
"%" & Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & _
"%" & Hex(nAsc And &H3F Or &H80)
sResult
= sResult & uChar
End If
End If
Next

GBToUTF8
= sResult
End Function

Function URLDecode(enStr)
Dim deStr, sSpecial, c, i, v

deStr
= ""
sSpecial
= "!""#$%&'()*+,.-_/:;<=>?@[\]^`{|}~%"
For i = 1 To Len(enStr)
c
= Mid(enStr, i, 1)
If c = "%" Then
v
= Eval("&h" + Mid(enStr, i + 1, 2))
If InStr(sSpecial, Chr(v)) > 0 Then
deStr
= deStr & Chr(v)
i
= i + 2
Else
v
= Eval("&h" + Mid(enStr, i + 1, 2) + Mid(enStr, i + 4, 2))
deStr
= deStr & Chr(v)
i
= i + 5
End If
Else
If c = "+" Then
deStr
= deStr & " "
Else
deStr
= deStr & c
End If
End If
Next

URLDecode
= deStr
End Function
使用实例
Dim sTest
sTest = GBQueryString("Test") '取得URL传过来的名为Test的参数的内容
Response.Write(sTest) '输出Test的内容到页面上
posted @ 2010-11-11 18:19  顺德早茶  阅读(693)  评论(0)    收藏  举报