ASP接收URL中utf-8编码的值
作者:刚子 日期:2010-01-27
用Request接收网址中utf-8编码值的时候是乱码,所以我开始使用adodb.stream来转码。后来发现一个问题就是当这个值是汉字并且位数是奇数的时候无法成功转码,如果是偶数的时候一切正常。于是乎我在csdn发了帖子,有两位哥们帮我详细分析了原因并提供了代码,代码非常正常,我相信也是那位哥们原创,估计是首发。发到这里的意思是希望能够帮助更多人解决这个问题。
下面贴出CSDN帖子的原地址,愿意继续研究的可以去看看:http://topic.csdn.net/u/20100126/20/badca4a8-2ed4-4ba5-8113-5a96449f6c6a.html?seed=507223990&r=63024055#r_63024055
程序代码
下面贴出CSDN帖子的原地址,愿意继续研究的可以去看看:http://topic.csdn.net/u/20100126/20/badca4a8-2ed4-4ba5-8113-5a96449f6c6a.html?seed=507223990&r=63024055#r_63024055
程序代码<%
Class StringList
Private dict, strName, i
Private Sub Class_Initialize()
Set dict = CreateObject("Scripting.Dictionary")
i = 0
End Sub
Public Property Get Count()
Count = i
End Property
Public Property Let Name(newValue)
strName = newValue
End Property
Public Property Get Name()
Name = strName
End Property
Public Sub Add(strValue)
i = i + 1
dict.Add i, strValue
End Sub
Public Default Property Get ToString()
ToString = Me.Item(Empty)
End Property
Public Property Get Item(index)
If Not IsEmpty(index) And IsNumeric(index) Then
If index<1 Then Err.Raise -1, "StringList.Item", "下标越界"
If index>i Then Err.Raise -1, "StringList.Item", "下标越界"
Item = dict.Item(index)
ElseIf i>0 Then
Item = Join(dict.Items(), ", ")
End If
End Property
End Class
Function decodeURIComponent(str, cSet)
With Server.CreateObject("ADODB.Stream")
.Type = 2
.Charset = "iso-8859-1"
.Open
.WriteText UnEscape(Replace(str, "+", "%20"))
.Position = 0
.Charset = cSet
decodeURIComponent = .ReadText(-1)
.Close
End With
End Function
Function getParameter(name, cSet, dictionary)
Dim match : Set getParameter = New StringList : getParameter.Name = name
With New RegExp
.Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
.Global = True
For Each match In .Execute(dictionary)
getParameter.Add decodeURIComponent(match.Submatches.Item(0), cSet)
Next
End with
End Function
%>
<%=getParameter("p", "UTF-8", Request.QueryString)%>
Class StringList
Private dict, strName, i
Private Sub Class_Initialize()
Set dict = CreateObject("Scripting.Dictionary")
i = 0
End Sub
Public Property Get Count()
Count = i
End Property
Public Property Let Name(newValue)
strName = newValue
End Property
Public Property Get Name()
Name = strName
End Property
Public Sub Add(strValue)
i = i + 1
dict.Add i, strValue
End Sub
Public Default Property Get ToString()
ToString = Me.Item(Empty)
End Property
Public Property Get Item(index)
If Not IsEmpty(index) And IsNumeric(index) Then
If index<1 Then Err.Raise -1, "StringList.Item", "下标越界"
If index>i Then Err.Raise -1, "StringList.Item", "下标越界"
Item = dict.Item(index)
ElseIf i>0 Then
Item = Join(dict.Items(), ", ")
End If
End Property
End Class
Function decodeURIComponent(str, cSet)
With Server.CreateObject("ADODB.Stream")
.Type = 2
.Charset = "iso-8859-1"
.Open
.WriteText UnEscape(Replace(str, "+", "%20"))
.Position = 0
.Charset = cSet
decodeURIComponent = .ReadText(-1)
.Close
End With
End Function
Function getParameter(name, cSet, dictionary)
Dim match : Set getParameter = New StringList : getParameter.Name = name
With New RegExp
.Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
.Global = True
For Each match In .Execute(dictionary)
getParameter.Add decodeURIComponent(match.Submatches.Item(0), cSet)
Next
End with
End Function
%>
<%=getParameter("p", "UTF-8", Request.QueryString)%>
评论: 0 | 引用: 0 | 查看次数: -
发表评论
上一篇
下一篇

文章来自:
Tags: