设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 3474|回复: 5
打印 上一主题 下一主题

ASP 接收参数并自动识别编码 (UTF/GBK) [复制链接]

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
Alvin 实名认证  发表于 2011-3-26 22:51:06 |只看该作者 |倒序浏览
直接上代码
  1. <%
  2. Function GetParameter(ByVal name, ByRef dictionary)
  3.     Dim values(), matches, match, i, cSet : cSet = "GBK"
  4.     With New RegExp
  5.         .Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
  6.         .Global = True
  7.         .IgnoreCase = True
  8.         Set matches = .Execute(dictionary)
  9.         ReDim values(matches.Count-1)
  10.         For i=0 To matches.Count-1
  11.             values(i) = matches(i).Submatches.Item(0)
  12.         Next
  13.         GetParameter = Unescape(Replace(Join(values, ", "), "+", " "))
  14.         ' .Pattern = "^(?:[\x00-\x7f]|[\xfc-\xff][\x80-\xbf]{5}|[\xf8-\xfb][\x80-\xbf]{4}|[\xf0-\xf7][\x80-\xbf]{3}|[\xe0-\xef][\x80-\xbf]{2}|[\xc0-\xdf][\x80-\xbf])+$"
  15.         .Pattern = "^(?:[\x00-\x7f]|[\xe0-\xef][\x80-\xbf]{2})+$"
  16.         If .Test(GetParameter) Then cSet = "UTF-8"
  17.     End with
  18.     With Server.CreateObject("ADODB.Stream")
  19.         .Type = 2
  20.         .Charset = "iso-8859-1"
  21.         .Open
  22.         .WriteText GetParameter
  23.         .Position = 0
  24.         .Charset = cSet
  25.         GetParameter = .ReadText(-1)
  26.         .Close
  27.     End With
  28. End Function

  29. Function QueryString(ByVal name)
  30.     QueryString = GetParameter(name, Request.QueryString)
  31. End Function

  32. Function Form(ByVal name)
  33.     Form = GetParameter(name, Request.Form)
  34. End Function

  35. %>
  36. <%=QueryString("gbkParam")%><br />
  37. <%=QueryString("utfParam")%><br />
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

Rank: 8Rank: 8

2#
momo 发表于 2011-3-26 23:48:16 |只看该作者
{:3_59:}前来拜读!
过了爱做梦的年纪
轰轰烈烈不如平静

使用道具 举报

Rank: 6Rank: 6

3#
Yisin 发表于 2011-3-27 23:45:25 |只看该作者
我看不懂啊
{:3_47:}
路不好走,你却依旧满眼的爱,找不到理由...

使用道具 举报

Rank: 1

4#
exitdos 发表于 2011-3-28 11:25:18 |只看该作者
这个我也测试了,放在uft-8的页面里,也是识别不了gb2312传递过来的中文编码啊。希坛主有空看看。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

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

我测试了呀,使用下面代码
并且访问
http://localhost/test.asp?wd1=%E4%B8%AD%E6%96%87&wd2=%D6%D0%CE%C4
wd1,wd2 都接收成功
如果还有问题,可以加我QQ 78423497
  1. <%@ language="vbscript" codepage="65001"%>
  2. <%
  3. Response.CharSet = "UTF-8"
  4. Response.CodePage = 65001

  5. Function GetParameter(ByVal name, ByRef dictionary)
  6.     Dim values(), matches, match, i, cSet : cSet = "GBK"
  7.     With New RegExp
  8.         .Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
  9.         .Global = True
  10.         .IgnoreCase = True
  11.         Set matches = .Execute(dictionary)
  12.         ReDim values(matches.Count-1)
  13.         For i=0 To matches.Count-1
  14.             values(i) = matches(i).Submatches.Item(0)
  15.         Next
  16.         GetParameter = Unescape(Replace(Join(values, ", "), "+", " "))
  17.         .Pattern = "^(?:[\x00-\x7f]|[\xe0-\xef][\x80-\xbf]{2})+$"
  18.         If .Test(GetParameter) Then cSet = "UTF-8"
  19.     End with
  20.     With Server.CreateObject("ADODB.Stream")
  21.         .Type = 2
  22.         .Charset = "iso-8859-1"
  23.         .Open
  24.         .WriteText GetParameter
  25.         .Position = 0
  26.         .Charset = cSet
  27.         GetParameter = .ReadText(-1)
  28.         .Close
  29.     End With
  30. End Function

  31. Function QueryString(ByVal name)
  32.     QueryString = GetParameter(name, Request.QueryString)
  33. End Function

  34. Function Form(ByVal name)
  35.     Form = GetParameter(name, Request.Form)
  36. End Function


  37. Response.Write QueryString("wd1") & "<br/>"
  38. Response.Write QueryString("wd2")

  39. %>
复制代码

使用道具 举报

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

手机版|Scripts 学盟   |

GMT+8, 2024-4-29 00:17 , Processed in 1.108706 second(s), 13 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部