欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP读取Request.QueryString编码的函数代码

程序员文章站 2022-06-21 22:38:40
1. 支持参数纯汉字 ?a=深山老熊 2. 支持参数gb2312 urlencode编码: ?a=%c9%ee%c9%bd%c0%cf%d0%dc 3. 支持参数utf-8...
1. 支持参数纯汉字 ?a=深山老熊
2. 支持参数gb2312 urlencode编码: ?a=%c9%ee%c9%bd%c0%cf%d0%dc
3. 支持参数utf-8 urlencode编码: ?a=%e6%b7%b1%e5%b1%b1%e8%80%81%e7%86%8a
复制代码 代码如下:

<%@language="vbscript" codepage="65001"%>
<%
option explicit
const yxcms_charset = "utf-8"
const yxcms_codepage = 65001
response.charset = "utf-8"
session.codepage = 65001
'测试url
'?n1=深山老熊&n2=%c9%ee%c9%bd%c0%cf%d0%dc&n3=%e6%b7%b1%e5%b1%b1%e8%80%81%e7%86%8a
'深山老熊
'gbk : %c9%ee%c9%bd%c0%cf%d0%dc
'utf-8 : %e6%b7%b1%e5%b1%b1%e8%80%81%e7%86%8a
dim uri,key
set uri = new cls_uri
'输出所有参数测试
for each key in uri.querystring
response.write "<span style='color:red'>" & key & " : </span>" & uri.get(key) & "<hr/>"
next
'取单个值
'uri.get("名称")
'--------------------------------------------
'asp utf-8编码下通吃 gbk utf-8编码
'作者: 深山老熊 qq:81090
'--------------------------------------------
class cls_uri
private o_item,o_regx
private url_query
private sub class_initialize
set o_item = createobject("scripting.dictionary")
o_item.comparemode = 1
set o_regx = new regexp
o_regx.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])+$"
session.codepage = 936
url_query = request.servervariables("query_string")
session.codepage = 65001
'根据query_string字符串,生成模拟querystring值
dim i,a,b,c,n,v
a = split(url_query,"&") : c = ubound(a)
for i = 0 to c
b = split(a(i),"=",2)
n = trim(b(0) & "")
if ubound(b) < 1 then
v = ""
else
v = b(1)
if instr(v,"%") > 0 then v = urldecode(v)
end if
if n <> "" then
o_item(n) = v
end if
next
set o_regx = nothing
end sub
private sub class_terminate
set o_item = nothing
end sub
'模拟 request.querystring
public function querystring()
set querystring = o_item
end function
'模拟 request.querystring(n)
public function [get](n)
if o_item.exists(n) then
[get] = o_item.item(n)
else
[get] = ""
end if
end function
'编码格式化
private function urldecode(byval s)
dim sm,cs,r,o_regex
if trim(s & "") = "" then
urldecode = s : exit function
end if
s = unescape(s)
if o_regx.test(s) then
cs = "utf-8"
else
cs = "gbk"
end if
set sm = createobject("adodb.stream")
with sm
.type = 2
.mode = 3
.open
.charset = "iso-8859-1"
.writetext s
.position = 0
.charset = cs
urldecode = .readtext(-1)
.close
end with
set sm = nothing
end function
end class
%>