日期:2014-05-17  浏览次数:20966 次

asp转码问题
这个问题困扰我很久了,希望csdn的高手能够帮助解决。
我需要用URL来传值,这个值是UTF-8编码的,比如“%E6%88%91%E7%88%B1%E4%BD%A0”,汉字就是“我爱你”。
理论上来讲我们用Request接收到这个值之后,将它转换成gb2312就行了。可是问题就在这里了,我用Request接收%E6%88%91%E7%88%B1%E4%BD%A0这个值的时候结果就成了“鎴戠埍浣”,而我用一个utf-8转gb的代码转“%E6%88%91%E7%88%B1%E4%BD%A0”这个值,可以正常转换。但是如果转换接收到的这个“鎴戠埍浣”就无法转换,也是乱码。

问题说清楚了,我想常用asp的人应该知道这个问题。简单的讲就是我需要接受URL中一个UTF-8的值,接受之后转为gb2312。
(对了,不可以在传值之前先转码)

------解决方案--------------------
假设你得主机编码为gbk,那么不管什么数据收来都会按gbk解码,所以如果出现乱码的话,首先要还原成原来的数据,然后再用正常的解码方式处理

'还原为原数据
set objAds=createobject("adodb.stream")
objAds.Type=2
objAds.mode=3
objAds.charset="gbk" '<---你主机的编码
objAds.open
objAds.writeText "鎴戠埍浣" '<---乱码,或者直接objAds.writeText request("xxx")
'这时objAds为没有编码过的原二进制数据了,采用正确解码
objAds.postion=0
objAds.charset="utf-8"
objAds.Positon=0
response.write objAds.readText() '<结果


ps:lz换个头像把,男人看男人怎么看都恶心
------解决方案--------------------
VBScript code
<%
Class StringList
    Private dict, strName, i

    Private Sub Class_Initialize()
        Set dict = CreateObject("Scripting.Dictionary")
        i = 0
    End Sub
    
    Public Property Get Count()
        Count = i
    End Property
    
    Public Property Let Name(newValue)
        strName = newValue
    End Property
    
    Public Property Get Name()
        Name = strName
    End Property
    
    Public Sub Add(strValue)
        i = i + 1
        dict.Add i, strValue
    End Sub
    
    Public Default Property Get ToString()
        ToString = Me.Item(Empty)
    End Property
    
    Public Property Get Item(index)
        If Not IsEmpty(index) And IsNumeric(index) Then
            If index<1 Then Err.Raise -1, "StringList.Item", "下标越界"
            If index>i Then Err.Raise -1, "StringList.Item", "下标越界"
            Item = dict.Item(index)
        ElseIf i>0 Then
            Item = Join(dict.Items(), ", ")
        End If
    End Property
End Class

Function decodeURIComponent(str, cSet)
    With Server.CreateObject("ADODB.Stream")
        .Type = 2
        .Charset = "iso-8859-1"
        .Open
        .WriteText UnEscape(Replace(str, "+", "%20"))
        .Position = 0
        .Charset = cSet
        decodeURIComponent = .ReadText(-1)
        .Close
    End With
End Function

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


%>
<%=getParameter("p", "UTF-8", Request.QueryString)%><br />
<%=getParameter("p", "UTF-8", Request.QueryString).Count%><br />
<%=getParameter("p", "UTF-8", Request.QueryString).Item(1)%><br />


<!--


Dim p
Set p = getParameter("p", "UTF-8", Request.QueryString)
p.Count
p.Item(1)

p.Count 和 p.Item(index)  是在当出现同名参数时 使用.

如果不考虑同名参数,则代码可以进行大幅删减

-->