日期:2014-05-16  浏览次数:20960 次

为什么asp中用xmlHttp抓取到的页面局部有乱码?
Function xmlHttp(sUrl, sCharSet)
    On Error Resume Next
    Dim xml: set xml = Server.CreateObject("Microsoft.XMLHTTP")
    xml.Open "GET", sUrl, False
    xml.setRequestHeader "Content-Type", "text/html;charset=" & sCharSet
    xml.Send()
    If Err.Number <> 0 Then 
        xmlHttp = ""
        Exit Function
    End If
    If xml.readyState = 4 Then
        xmlHttp = BytesToBstr(xml.responseBody, sCharSet)
    End If
End Function

Function BytesToBstr(cnvUni, sCharSet)
    On Error Resume Next
    Dim objStream: set objStream = Server.CreateObject("adodb.stream") 
    With objStream
        .Type = 1
        .Mode = 3
        .Open
        .Write cnvUni
        .Position = 0
        .Type = 2
        .Charset =  sCharSet
        BytesToBstr = .ReadText
        .Close
    End With
End Function

url="http://proxy.ncuhome.cn/surf.aspx"
strContent=xmlHttp(url, "gb2312")
response.write strContent


结果显示的大部分是正确的,没有乱码,但是少部分出现乱码(出现乱码的情况见下边的图片)

关键是大多数的中文字都是没问题的啊,只有少部分有问题
我把GB2312改为UTF-8还是不行的

高手帮帮我,我是菜鸟,太感谢了


------解决方案--------------------
用这个函数转换下即可
 'S=http.responseBody
function getGBKTEXT(S)
     
            DIM text;
            SET adoS=SERVER.CREATEOBJECT("ADODB.Stream")
            adoS.Charset="gb2312"
            adoS.Type=1  //设置为二进制
            adoS.mode=3  //设置可读写
            adoS.open
            var txt=S
            adoS.Write(txt)  //用二进制写
        &