zoukankan      html  css  js  c++  java
  • ajax 乱码的解决

    那么如何偷呢?看过XMLHTTP介绍的应该了解些。

    先定义了一个函数,解释在函数中:


    <%
    Function getHTTPPage(url)
        dim objXML
        set objXML=server.createobject("MSXML2.XMLHTTP")'定义
        objXML.open "GET",url,false'打开
        objXML.send()'发送
        If objXML.readystate<>4 then '判断文档是否已经解析完,以做客户端接受返回消息
            exit function
        End If
        getHTTPPage=BytesToBstr(objXML.responseBody)'返回信息,同时用函数定义编码
        'getHTTPPage=bytes2BSTR(objXML.responseBody)'或者返回信息时用函数转换汉字
        set objXML=nothing'关闭
        if err.number<>0 then err.Clear
    End Function
    %>

    接着就来看看定义编码函数BytesToBstr()的主要内容


    <%
    Function BytesToBstr(body)
    dim objstream
    set objstream = Server.CreateObject("adodb.stream")
        objstream.Type = 1
        objstream.Mode =3
        objstream.Open
        objstream.Write body
        objstream.Position = 0
        objstream.Type = 2
        objstream.Charset = "GB2312" 
        '转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
        BytesToBstr = objstream.ReadText
    objstream.Close
    set objstream = nothing
    End Function
    %>

    当然,还可以使用专门的函数来处理汉字:


    Function bytes2BSTR(vIn)
    strReturn = ""
    For j = 1 To LenB(vIn)
        ThisCharCode = AscB(MidB(vIn,j,1))
        If ThisCharCode < &H80 Then
            strReturn = strReturn & Chr(ThisCharCode)
        Else
            NextCharCode = AscB(MidB(vIn,j+1,1))
            strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
            j = j + 1
        End If
    Next
    bytes2BSTR = strReturn
    End Function

    lenB返回字节数而不是字符数,同理ascB返回每个字节的ascii码,大于80h,也就是128的ascii是汉字——半个汉字,把半个半个的汉字ascii码拼合再用chr函数返回字符就可以了。(进行移位&H100 =256往前移8位)

    应用则如下:


    <%
    Dim Url,Html
    Url = "http://www.cnbruce.com/blog"
    Html = getHTTPPage(Url)
    response.write(Html)
    %>

    即表示对 http://www.cnbruce.com/blog 的内容进行“偷”行为,返回了相关该站的内容。

    拷贝下列内容保持为ASP文件进行调试
    程序代码: [ 复制代码到剪贴板 ] 
    <%
    Function getHTTPPage(url)
        dim objXML
        set objXML=server.createobject("MSXML2.XMLHTTP")'定义
        objXML.open "GET",url,false'打开
        objXML.send()'发送
        If objXML.readystate<>4 then '判断文档是否已经解析完,以做客户端接受返回消息
            exit function
        End If
        getHTTPPage=BytesToBstr(objXML.responseBody)'返回信息,同时用函数定义编码
        set objXML=nothing'关闭
        if err.number<>0 then err.Clear
    End Function

    Function BytesToBstr(body)
    dim objstream
    set objstream = Server.CreateObject("adodb.stream")
        objstream.Type = 1
        objstream.Mode =3
        objstream.Open
        objstream.Write body
        objstream.Position = 0
        objstream.Type = 2
        objstream.Charset = "GB2312" 
        '转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
        BytesToBstr = objstream.ReadText
    objstream.Close
    set objstream = nothing
    End Function

    Dim Url,Html
    Url = "http://www.cnbruce.com/blog"
    Html = getHTTPPage(Url)
    response.write(Html)
    %>

    这样一个页就被“偷”下来了。同时,注意到返回的信息中,有的图片不能显示,样式也不能连接,如要正常,则需要将返回的信息做过滤和调整。

  • 相关阅读:
    .NET CORE 技术债
    .Net Core/Framework之Nginx反向代理后获取客户端IP等数据探索
    托管ASP.NET Core应用程序到Windows服务中
    Office在线预览及PDF在线预览的实现方式大集合
    Ubuntu 16.10 虚拟机安装记录
    .Net Core 之 图形验证码
    Ubuntu之Mysql安装及基本设置
    .Net Core 之 Ubuntu 14.04 部署过程
    常用.net反编译替换正则表达式
    江湖救急:webbrowser中js文件丢失问题~
  • 原文地址:https://www.cnblogs.com/snowball/p/477708.html
Copyright © 2011-2022 走看看