网络大杂烩首页

网络大杂烩

  • 网站首页
  • HTML/CSS
  • javascript
  • seo优化
  • PS美工
  • 系统安全
  • 漏洞预警
  • 编程学习
  • 原创专区
  • 【网络大杂烩】是一家综合技术门户,为您提供网站建设,编程开发,安全维护,漏洞预警等技术文章

    技术文章MAP

    文章

    VB Inet1获取UTF-8编码的网页乱码的解决办法-附代码

    日期:2019/12/9 13:46:39来源分类:编程开发

    VB Inet1获取UTF-8编码的网页乱码的解决办法,以下代码测试通过:
    Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    Private Const CP_UTF8 = 65001
    Private Function Utf8ToUnicode(ByRef Utf() As Byte) As String
        Dim lRet As Long
        Dim lLength As Long
        Dim lBufferSize As Long
        lLength = UBound(Utf) - LBound(Utf) + 1
        If lLength <= 0 Then Exit Function
        lBufferSize = lLength * 2
        Utf8ToUnicode = String$(lBufferSize, Chr(0))
        lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
        If lRet <> 0 Then
            Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
        Else
            Utf8ToUnicode = ""
    End If
    End Function
    Private Sub Command1_Click()
        Dim aa() As Byte
        Dim t As String, t1 As String, t2 As String
        aa = Inet1.OpenURL(Text1.Text, icByteArray)      'URL在Text1里输入
        If IsUTF8(aa) Then
            t = Utf8ToUnicode(aa)
        Else
            t = StrConv(aa, vbUnicode)
    End If
        t1 = InStr(t, "<title>") + Len("<title>")
    t2 = InStr(t, "</title>")
        Text2.Text = Mid(t, t1, t2 - t1)
    End Sub
     '判断网页编码函数
    Private Function IsUTF8(Bytes) As Boolean
        Dim i As Long, AscN As Long, Length As Long
        Length = UBound(Bytes) + 1
        If Length < 3 Then
            IsUTF8 = False
            Exit Function
        ElseIf Bytes(0) = &HEF And Bytes(1) = &HBB And Bytes(2) = &HBF Then
            IsUTF8 = True
            Exit Function
        End If
        Do While i <= Length - 1
            If Bytes(i) < 128 Then
                i = i + 1
                AscN = AscN + 1
            ElseIf (Bytes(i) And &HE0) = &HC0 And (Bytes(i + 1) And &HC0) = &H80 Then
                i = i + 2
            ElseIf i + 2 < Length Then
                If (Bytes(i) And &HF0) = &HE0 And (Bytes(i + 1) And &HC0) = &H80 And (Bytes(i + 2) And &HC0) = &H80 Then
                     i = i + 3
                Else
                    IsUTF8 = False
                    Exit Function
                End If
            Else
                IsUTF8 = False
                Exit Function
            End If
        Loop
        If AscN = Length Then
            IsUTF8 = False
        Else
            IsUTF8 = True
        End If
    End Function

    随机推荐

    • 该分类还没有添加任何内容!
    • 该分类还没有添加任何内容!

    Copyright 2005-2019 【网络大杂烩】 版权所有 黑ICP备16886888号

    声明:本站所有文章来自互联网 如有异议 请联系本站管理员