VB获取Json文件错误:“远程服务器返回错误:(400)错误的请求." [英] VB Get Json File Error: 'The remote server returned an error: (400) Bad Request.'
本文介绍了VB获取Json文件错误:“远程服务器返回错误:(400)错误的请求."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码可在其他网页上使用,但此特定网页出现错误.任何帮助表示赞赏.
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
推荐答案
缺少User-Agent
标头会生成Bad request
.为了应用它,您需要将WebRequest
强制转换为HttpWebRequest
.
The absence of User-Agent
header generated Bad request
. In order to apply it, you need to cast WebRequest
to 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
这篇关于VB获取Json文件错误:“远程服务器返回错误:(400)错误的请求."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文