asp转码问题

go786 2009-07-01 06:19:25
我需要用GB2312编码的网页来接受UTF-8页面传来的值,开始是出现乱码,之后我从网上找个一个ASP转码函数。使用时候发现了这样的情况:
1.传来的值是英文是没问题的
2.传来的值如果是汉字的 并且位数为偶数 也可以正常转码 如果传来的汉字总数是奇数 那么无法转码 依然是乱码
求高手帮解 或者给我另外一个代码也行 下面贴出我的代码
<%   
Function utf2gb(Body)
Dim Objstream
Set Objstream = Server.CreateObject("adodb.stream")
objstream.Charset = "gb2312"
objstream.Type = 2
objstream.Mode =3
objstream.Open
objstream.WriteText body
objstream.Position = 0
objstream.Charset = "utf-8"
objstream.Type = 2
utf2gb = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
key=Request.QueryString("key")
key=utf2gb(key)
%>
...全文
681 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
copiy 2010-11-19
  • 打赏
  • 举报
回复
正好需要 谢谢啦
hqqstudy 2010-01-11
  • 打赏
  • 举报
回复
…………
mocom 2009-07-01
  • 打赏
  • 举报
回复
那个Server.Echo改成Response.Write
mocom 2009-07-01
  • 打赏
  • 举报
回复
解析Request.ServerVariables("QUERY_STRING")这个变量,生成指定编码的QueryString


Public QueryString
Set QueryString = Server.CreateObject("Scripting.Dictionary")
Call ProcessQueryString("utf-8")

Dim key
For Each key In QueryString.Keys
Server.Echo "QueryString(" & key & ") = " & QueryString(key)
Next

Public Sub ProcessQueryString(ByVal charset)
Dim qs, arr, ptr, tmp
qs = Request.ServerVariables("QUERY_STRING")
'qs = "as_q=%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD&hl=zh-CN&newwindow=1&num=10&btnG=Google+%E6%90%9C%E7%B4%A2&as_epq=&as_oq=&as_eq=&lr=lang_zh-CN&cr=&as_ft=i&as_filetype=&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=&as_rights="
arr = Split(qs, "&")
For Each ptr In arr
tmp = Split(ptr, "=", 2)
If UBound(tmp) = 1 Then
QueryString(URLDecode(tmp(0), charset)) = URLDecode(tmp(1), charset)
End If
Next
End Sub

Public Function URLDecode(ByVal data, ByVal charset)
Dim strm
Set strm = Server.CreateObject("ADODB.Stream")
With strm
.type = 2
.charset = "iso-8859-1"
.open
.writeText unescape(data)
.position = 0
.charset = charset
URLDecode = .readText(-1)
.close
End With
Set strm = Nothing
End Function
smalltaro 2009-07-01
  • 打赏
  • 举报
回复
p.s.escape是js下的函数
smalltaro 2009-07-01
  • 打赏
  • 举报
回复
我的理解:
你在gb2312编码的页面里
key=Request.QueryString("key")
经过这一步,相当于把utf-8的内容按gb2312来保存了,本身已经乱码了,再怎么解也没用

LS说的escape应该是指在发送页面, 发送数据的页面你可以修改的吗?
可以的话就在发送的用escape编码
smalltaro 2009-07-01
  • 打赏
  • 举报
回复
走偏门,哇哈哈

<%
Session.CodePage = 65001
key=Request.QueryString("key")
Session.CodePage = 936
Response.write key
%>
go786 2009-07-01
  • 打赏
  • 举报
回复
lzp4881 同学说的escape功能是什么哦?有没有具体例子 我刚从网上找了个escape函数试了下 发现还是乱码
go786 2009-07-01
  • 打赏
  • 举报
回复
2楼说的代码我也加了啊
lzp4881 2009-07-01
  • 打赏
  • 举报
回复
传过来之前用escape处理一下
x2xnet 2009-07-01
  • 打赏
  • 举报
回复
在头文件上加入<meta http-equiv="Content-Type" content="text/html; Charset = "gb2312" />,
这个问题我也碰到过。
go786 2009-07-01
  • 打赏
  • 举报
回复
mocom 同学的很帅 谢谢啦

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧