我正在使用Visual Basic向网页发出请求,我想解析响应并将其添加到数据库中。我的问题是,当我发出请求时,Windows会提示一个问题,询问我是否要保存文件。有没有办法直接在VB中以字符串形式接收响应?
以下是我的代码示例:
Dim ret As Long
Dim ie As Object
'checking connection state
ret = InternetGetConnectedStateEx(ret, sConnType, 254, 0)
'instantiating IE
Set ie = CreateObject("internetexplorer.application")
'hiding browser
ie.Visible = False
'opening link
Header = "x-api-key:..." & Chr(10) & Chr(13)
ie.Navigate "https://api.openapi.ro/api/companies/13548146", , , , Header
答案 0 :(得分:0)
如果这是VBA,我建议使用XMLHTTP。在VBA IDE中,使用工具--->引用在项目中添加对“Microsoft XML,v6.0”的引用(您可能有不同的版本,相应地调整代码和引用)。
以下是如何使用它的示例:
Public Sub TestIt()
Dim xh As XMLHTTP60
Set xh = New XMLHTTP60
With xh
.Open "GET", "https://jsonplaceholder.typicode.com/posts/1"
.send
End With
'In this case, the response text was in ASCII, so I had to use StrConv to convert it to Unicode for VBA.
Debug.Print StrConv(xh.responseBody, vbUnicode)
End Sub