Scripts 学盟

标题: ASP 接收参数并自动识别编码 (UTF/GBK) [打印本页]

作者: Alvin    时间: 2011-3-26 22:51:06     标题: ASP 接收参数并自动识别编码 (UTF/GBK)

直接上代码
  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 />
复制代码

作者: momo    时间: 2011-3-26 23:48:16

{:3_59:}前来拜读!
作者: Yisin    时间: 2011-3-27 23:45:25

我看不懂啊
{:3_47:}
作者: exitdos    时间: 2011-3-28 11:25:18

这个我也测试了,放在uft-8的页面里,也是识别不了gb2312传递过来的中文编码啊。希坛主有空看看。
作者: 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 学盟 (http://www.iscripts.org/) Powered by Discuz! X2