- Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
- Private Const CP_UTF8 = 65001
- 'Purpose:Convert Utf8 to Unicode
- Public Function UTF8_Decode(ByVal sUTF8 As String) As String
- Dim lngUtf8Size As Long
- Dim strBuffer As String
- Dim lngBufferSize As Long
- Dim lngResult As Long
- Dim bytUtf8() As Byte
- Dim n As Long
- If LenB(sUTF8) = 0 Then Exit Function
- On Error GoTo EndFunction
- bytUtf8 = StrConv(sUTF8, vbFromUnicode)
- lngUtf8Size = UBound(bytUtf8) + 1
- On Error GoTo 0
- lngBufferSize = lngUtf8Size * 2
- strBuffer = String$(lngBufferSize, vbNullChar)
- 'Translate using code page 65001(UTF-8)
- lngResult = MultiByteToWideChar(CP_UTF8, 0, bytUtf8(0), _
- lngUtf8Size, StrPtr(strBuffer), lngBufferSize)
- 'Trim result to actual length
- If lngResult Then
- UTF8_Decode = Left$(strBuffer, lngResult)
- End If
- End Function
把你上面獲取的內容轉一下就行,比如Text1.Text=UTF8_Decode(strResponse)
還可以加多一句InStr(1, strResponse, "charset=utf-8") > 0判斷是否是UTF8編碼再執行,不是UTF8的就直接顯示。 還有些網頁編碼那裏是大寫的,要這樣寫InStr(1, strResponse, "charset=UTF-8") > 0