上一篇的我找到的转换代码不太理想。任然有一些是“?”乱码,经过向多方学习最后找到两个方法。
第一种方法
设置一个按钮,一个文本框。文本框的Multiline属性设置为true
- Const URLaddr = "http://api.douban.com/book/subject/isbn/9787115157676" '豆瓣网获取书籍信息网页地址
- Dim IEread As Object
- Dim i&, jj&, aa$
- Private Sub Command1_Click()
- Set IEread = CreateObject("WinHttp.WinHttpRequest.5.1")
- CallByName IEread, "Open", VbMethod, "GET", URLaddr, True
- CallByName IEread, "Send", VbMethod
- CallByName IEread, "WaitForResponse", VbMethod
- aa = CallByName(IEread, "ResponseText", VbMethod)
- Text1.Text = aa
- End Sub
第二种方法
需要引用Microsoft XML,v2.6
- Function GetBody(urls as string, Charset as string)
- GetBody = ""
- On Error Resume Next
- Dim Ado_Stream As ADODB.Stream
- Dim Obj_XMLHTTP As MSXML2.XMLHTTP
- Set Obj_XMLHTTP = New MSXML2.XMLHTTP
- Set Ado_Stream = New ADODB.Stream
- Obj_XMLHTTP.Open "get", urls, False
- Obj_XMLHTTP.send
- If Obj_XMLHTTP.readyState = 4 Then
- If Charset = "" Then
- GetBody = Obj_XMLHTTP.responseBody
- Else
- Ado_Stream.Type = 1
- Ado_Stream.Mode = 3
- Ado_Stream.Open
- Ado_Stream.Write Obj_XMLHTTP.responseBody
- Ado_Stream.Position = 0
- Ado_Stream.Type = 2
- Ado_Stream.Charset = Charset
- GetBody = Ado_Stream.ReadText
- Ado_Stream.Close
- End If
- End If
- Set Ado_Stream = Nothing
- Set Obj_XMLHTTP = Nothing
- End Function
- Private Sub Command1_Click()
- Dim Txml As String
- Txml = GetBody("http://api.douban.com/book/subject/isbn/9787115157676" , "UTF-8")
- Debug.Print Txml
- End Sub