解决Inet控件下载utf-8网页乱码的问题
Inet控件(Internet Transfer Control控件)下载网页的HTML代码是很方便,不过有个问题,在读取的是utf-8编码的网页时会出现乱码。这也难怪VB默认支持的是UNICODE编码,在读取utf-8的数据时自然不知所措了。
(注:如果你还不知道上面所说的各种字符编码方式的意义的话,那您还是先阅读一下这篇文章:各种字符编码方式详解(ANSI,UNICODE,UTF-8,GB2312,GBK),如果您已经知道请继续)
那怎么才能将utf-8的网页数据转换成UNICODE呢?
首先Inet取得数据的时候必须用二进制的形式取得,比如:
异步调用:
Private Sub Command1_Click()
Dim StrUrl As String
StrUrl = Text1.Text
Inet1.Execute StrUrl, "GET"
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
If State = icResponseCompleted Then
Dim BinBuff() As Byte
BinBuff = Inet1.GetChunk(0, icByteArray)
End If
End Sub
同步调用:
Private Sub Command2_Click()
Dim BinBuff() As Byte
Dim StrUrl As String
StrUrl = Text1.Text
BinBuff = Inet1.OpenURL(Text1.Text, icByteArray)
RichTextBox1.Text = Utf8ToUnicode(BinBuff)
End Sub
上面是取得inet取得二进制数据的代码
添加模块
'utf-8转换UNICODE代码
Option Explicit
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
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
Utf8ToUnicode函数即可将Inet收到的二进制数组转换成UNICODE字符串。传入参数为二进制数组返回转换后的字符串。
上面的代码可为:
Private Sub Inet1_StateChanged(ByVal State As Integer)
If State = icResponseCompleted Then
Dim BinBuff() As Byte
BinBuff = Inet1.GetChunk(0, icByteArray)
RichTextBox1.Text = Utf8ToUnicode(BinBuff)
End If
End Sub
返回之后在RichTextBox1中显示,发现本来乱码的中文现在已经能正常显示了。