代码适用于其他网页但我在此特定网页上收到错误。任何帮助表示赞赏。
Private Function GetJson()
Dim JsonString As String
Dim request As Net.WebRequest = Net.WebRequest.Create("https://api-public.sandbox.gdax.com/products") 'set url
request.Method = "GET"
Dim response As Net.WebResponse = request.GetResponse()
Dim inputstream1 As IO.Stream = response.GetResponseStream() ' define the stream
Dim reader As New IO.StreamReader(inputstream1) ' get the data stream set
JsonString = reader.ReadToEnd 'saves stream in jsonstring
ProgressTxtBx.Text = JsonString
'Dim read = Newtonsoft.Json.Linq.JObject.Parse(JsonString) 'Parsed
inputstream1.Dispose() ' CLEAN UP
reader.Close() '
response.Close() '
End Function
答案 0 :(得分:1)
缺少User-Agent
标头生成Bad request
。要应用它,您需要将WebRequest
投射到HttpWebRequest
。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim JsonString$
Dim req = DirectCast(Net.WebRequest.Create("https://api-public.sandbox.gdax.com/products"), HttpWebRequest)
req.Method = "GET"
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.15 Safari/537.36 OPR/51.0.2830.0 (Edition developer)"
Using resp = req.GetResponse()
Using strm = resp.GetResponseStream()
Using sr = New StreamReader(strm)
JsonString = sr.ReadToEnd()
End Using
End Using
End Using
Dim json = Newtonsoft.Json.Linq.JObject.Parse(JsonString)
End Sub
答案 1 :(得分:0)
我相信您可以捕获WebExceptions以获取服务器返回的任何其他错误信息。
Dim response As Net.WebResponse
Try
response = request.GetResponse()
Catch ex As WebException
If ex.Response IsNot Nothing...
response = ex.Response
End If
End Try