设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 3645|回复: 2
打印 上一主题 下一主题

ASP 接收不同编码提交的参数 [复制链接]

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
Alvin 实名认证  发表于 2011-3-26 16:39:15 |只看该作者 |倒序浏览
utf-8页面,接收gbk参数, 或者 gbk 页面接收 utf-8 参数,或者...
很少会出现这样的参求,但有时候却是不得己

比如说合作网站提交过来的数据。。。

e, 不瞎扯,直接代码
  1. <script language="jscript" runat="Server">
  2. function decodeURL(str, cSet) {
  3.         with (Server.CreateObject("ADODB.Stream")) {
  4.                 Type=2; Charset="iso-8859-1"; Open(); WriteText(unescape(str.replace(/\+/g, "%20")));
  5.                 Position=0; Charset=cSet; str=ReadText(-1); Close();
  6.         }
  7.         return str;
  8. }
  9. function getParameter(paramName, encodingForDecode, dictionary) {
  10.         var values = [];
  11.         dictionary.Item.replace(new RegExp("(?:^|&)" + paramName + "=([^&]*)", "gi"), function($, $1) {
  12.                 values.push(decodeURL($1, encodingForDecode));
  13.         });
  14.         return values;
  15. }
  16. </script>
  17. <%=getParameter("gbkParam", "GBK", Request.QueryString)%><br />
  18. <%=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
  1. <%
  2. Class StringList
  3.     Private dict, strName, i

  4.     Private Sub Class_Initialize()
  5.         Set dict = CreateObject("Scripting.Dictionary")
  6.         i = 0
  7.     End Sub
  8.    
  9.     Public Property Get Count()
  10.         Count = i
  11.     End Property
  12.    
  13.     Public Property Let Name(newValue)
  14.         strName = newValue
  15.     End Property
  16.    
  17.     Public Property Get Name()
  18.         Name = strName
  19.     End Property
  20.    
  21.     Public Sub Add(strValue)
  22.         i = i + 1
  23.         dict.Add i, strValue
  24.     End Sub
  25.    
  26.     Public Default Property Get ToString()
  27.         ToString = Me.Item(Empty)
  28.     End Property
  29.    
  30.     Public Property Get Item(index)
  31.         If Not IsEmpty(index) And IsNumeric(index) Then
  32.             If index<1 Then Err.Raise -1, "StringList.Item", "下标越界"
  33.             If index>i Then Err.Raise -1, "StringList.Item", "下标越界"
  34.             Item = dict.Item(index)
  35.         ElseIf i>0 Then
  36.             Item = Join(dict.Items(), ", ")
  37.         End If
  38.     End Property
  39. End Class

  40. Function decodeURIComponent(str, cSet)
  41.     With Server.CreateObject("ADODB.Stream")
  42.         .Type = 2
  43.         .Charset = "iso-8859-1"
  44.         .Open
  45.         .WriteText UnEscape(Replace(str, "+", "%20"))
  46.         .Position = 0
  47.         .Charset = cSet
  48.         decodeURIComponent = .ReadText(-1)
  49.         .Close
  50.     End With
  51. End Function

  52. Function getParameter(name, cSet, dictionary)
  53.     Dim match : Set getParameter = New StringList : getParameter.Name = name
  54.     With New RegExp
  55.         .Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
  56.         .Global = True
  57.         For Each match In .Execute(dictionary)
  58.             getParameter.Add decodeURIComponent(match.Submatches.Item(0), cSet)
  59.         Next
  60.     End with
  61. End Function


  62. %>
  63. <%=getParameter("gbkParam", "GBK", Request.QueryString)%><br />
  64. <%=getParameter("utfParam", "UTF-8", Request.QueryString)%><br />
  65. <%=getParameter("utfParam", "UTF-8", Request.QueryString).Count%><br />
  66. <%=getParameter("utfParam", "UTF-8", Request.QueryString).Item(1)%><br />
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

相关帖子

Rank: 1

2#
exitdos 发表于 2011-3-28 11:24:02 |只看该作者
坛主,这个好像用不了啊。
我utf-8页面,接收gb2312编码参数,还是不行。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
Alvin 实名认证  发表于 2011-3-28 12:44:07 |只看该作者
回复 2# exitdos

具体什么情况QQ啊
78423497

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-5 16:46 , Processed in 1.065213 second(s), 13 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部