Scripts 学盟
标题:
ASP 接收不同编码提交的参数
[打印本页]
作者:
Alvin
时间:
2011-3-26 16:39:15
标题:
ASP 接收不同编码提交的参数
utf-8页面,接收gbk参数, 或者 gbk 页面接收 utf-8 参数,或者...
很少会出现这样的参求,但有时候却是不得己
比如说合作网站提交过来的数据。。。
e, 不瞎扯,直接代码
<script language="jscript" runat="Server">
function decodeURL(str, cSet) {
with (Server.CreateObject("ADODB.Stream")) {
Type=2; Charset="iso-8859-1"; Open(); WriteText(unescape(str.replace(/\+/g, "%20")));
Position=0; Charset=cSet; str=ReadText(-1); Close();
}
return str;
}
function getParameter(paramName, encodingForDecode, dictionary) {
var values = [];
dictionary.Item.replace(new RegExp("(?:^|&)" + paramName + "=([^&]*)", "gi"), function($, $1) {
values.push(decodeURL($1, encodingForDecode));
});
return values;
}
</script>
<%=getParameter("gbkParam", "GBK", Request.QueryString)%><br />
<%=getParameter("utfParam", "UTF-8", Request.QueryString)%><br />
复制代码
VBScript 实现
测试地址:http://localhost/1.asp?gbkParam=%D6%D0%CE%C4%B0%A1&utfParam=%E4%B8%AD%E6%96%87%E5%95%8A
<%
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("gbkParam", "GBK", Request.QueryString)%><br />
<%=getParameter("utfParam", "UTF-8", Request.QueryString)%><br />
<%=getParameter("utfParam", "UTF-8", Request.QueryString).Count%><br />
<%=getParameter("utfParam", "UTF-8", Request.QueryString).Item(1)%><br />
复制代码
作者:
exitdos
时间:
2011-3-28 11:24:02
坛主,这个好像用不了啊。
我utf-8页面,接收gb2312编码参数,还是不行。
作者:
Alvin
时间:
2011-3-28 12:44:07
回复
2#
exitdos
具体什么情况QQ啊
78423497
欢迎光临 Scripts 学盟 (http://www.iscripts.org/)
Powered by Discuz! X2