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编码的网页乱码 展开
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编码的网页乱码 展开
1个回答
展开全部
转化一下就可以,以下代码测试通过:
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
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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
金山毒霸
2024-10-31 广告
2024-10-31 广告
电脑在启动游戏或软件的时候,桌面弹出错误提示框“0xc0000094”,应该怎么办?电脑报错,很大可能是因为某些DLL文件出现了异常,比如感染了病毒、DLL文件丢失等。解决方法如下:一、 手动解决方法1、先确定电脑操作系统是多少位的,现在的...
点击进入详情页
本回答由金山毒霸提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询