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

VB Get Json文件错误:“远程服务器返回错误:(400)请求错误。”,json,vb.net,Json,Vb.net,代码可以在其他网页上运行,但我在这个特定网页上遇到了一个错误。感谢您的帮助 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

代码可以在其他网页上运行,但我在这个特定网页上遇到了一个错误。感谢您的帮助

    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
缺少用户代理标头会生成错误的请求。为了应用它,您需要将WebRequest强制转换为HttpWebRequest


我相信您可以捕获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 
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