简体   繁体   English

VB获取Json文件错误:“远程服务器返回错误:(400)错误的请求。”

[英]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 . 为了应用它,您需要将WebRequestHttpWebRequest

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.

相关问题 远程服务器返回错误:(400) 错误请求。 使用 Shopify API 发布带有 body_html 的产品 - The remote server returned an error: (400) Bad Request. Using Shopify API to post product with body_html 远程服务器返回错误:(400)错误请求? - The remote server returned an error: (400) Bad Request? HttpWebResponse:远程服务器返回错误:(400) 错误请求 - HttpWebResponse: The remote server returned an error: (400) Bad Request WCF REST POST JSON-远程服务器返回错误:(400)错误的请求 - WCF REST POST JSON - The remote server returned an error: (400) Bad Request QPX Express Airfare API返回远程服务器返回错误:(400)错误的请求 - QPX Express Airfare API return The remote server returned an error: (400) Bad Request c# 获取 oauth2 令牌但得到错误(远程服务器返回 400 Bad Request) - c# to get oauth2 token but get error (Remote server returns 400 Bad Request) file_get_contents抛出400 Bad Request错误PHP - file_get_contents throws 400 Bad Request error PHP JSON数据400错误的请求错误 - JSON data 400 Bad request Error 我正在处理json并收到错误,返回响应状态为400 Bad Request - I am working on json and getting the error returned a response status of 400 Bad Request 400(错误请求)错误 - 400 (Bad Request) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM