[英]VB Get Json File Error: 'The remote server returned an error: (400) Bad Request.'
Code works on other web pages but i get an error with this particular web page. 代码可在其他网页上运行,但此特定网页出现错误。 any help is appreciated.
任何帮助表示赞赏。
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
The absence of User-Agent
header generated Bad request
. 没有
User-Agent
标头会生成Bad request
。 In order to apply it, you need to cast WebRequest
to HttpWebRequest
. 为了应用它,您需要将
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
I believe you could trap for WebExceptions to get at any additional error information returned by the server. 我相信您可以捕获WebException,以获取服务器返回的任何其他错误信息。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.