VB Inet获取UTF-8编码的网页乱码

Dimaa$aa=Inet1.OpenURL(localhost/index.html)'发布了完整的URL,laji知道,一发URL就删问题,白浪费我分t1=InStr... Dim aa$
aa = Inet1.OpenURL(localhost/index.html)'发布了完整的URL, laji知道, 一发URL就删问题, 白浪费我分
t1 = InStr(aa, "<title>") + Len("<title>")
t2 = InStr(aa, "</title>")
MsgBox Mid(aa, t1, t2 - t1)
我用着获取GBK的可以正常显示,单是如何获取UTF-8编码的网页出来就是乱码 求解决?最好把代码贴出来, 复制的别来, 我在搜索里找了很多都不行

VB Inet获取UTF-8编码的网页乱码
展开
 我来答
度老虎
推荐于2018-04-12 · TA获得超过1546个赞
知道大有可为答主
回答量:1661
采纳率:100%
帮助的人:2318万
展开全部
转化一下就可以,以下代码测试通过:
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
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式