简体   繁体   English

使用WebClient和C#,即使响应是(400)错误请求,如何获取返回的数据?

[英]Using a WebClient and C#, how do I get returned data even when the response is (400) Bad Request?

I am using the Google Translate API and am trying to capture the data returned when I get an error . 我正在使用谷歌翻译API,并试图捕获我收到错误时返回的数据。 (FYI: I know the API Key is wrong, I am just testing this). (仅供参考:我知道API密钥错了,我只是在测试它)。

The issue is that the browser, as you can see by clicking the link, displays the error info, but C# throws a WebException and I can't seem to get the response data. 问题是,通过单击链接可以看到浏览器显示错误信息,但C#抛出WebException,我似乎无法获取响应数据。

Here is my code: 这是我的代码:

string url = "https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world";
WebClient clnt = new WebClient();

//Get string response
try
{
    strResponse = clnt.DownloadString(url);
    System.Diagnostics.Debug.Print(strResponse);
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show(ex.Message);
    return null;
}

How do I get the JSON error returned even when the response is a (400) Bad Request (or any other error resonse for that matter)? 即使响应是(400)错误请求(或任何其他错误共振),如何返回JSON错误? Do I need to use different classes other than a WebClient ? 我是否需要使用WebClient以外的其他类?

This may help you 这可能对你有所帮助

catch ( WebException exception )
{
   string responseText;

   using(var reader = new StreamReader(exception.Response.GetResponseStream()))
   {
     responseText = reader.ReadToEnd();
   }
}

That will get you the json text, that you can then convert from JSON using whichever method you prefer. 这将获得json文本,然后您可以使用您喜欢的任何方法从JSON转换。

Retrieved from: Get WebClient errors as string 取自: 以字符串形式获取WebClient错误

I would catch the specific exception you are receiving - it will have pertinent data concerning the failure. 我会抓住你收到的具体例外情况 - 它会有关于失败的相关数据。

According to MSDN, WebException.Response will contain the response received from the server. 根据MSDN, WebException.Response将包含从服务器收到的响应。

Once you are able to retrieve the JSON data from this response object, then you'll need to deserialize it yourself if you want. 一旦能够从此响应对象检索JSON数据,您就需要自己反序列化它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中调用REST API时,返回错误请求[400] - Bad Request [400] returned when REST API is called in C# ConvertAPI-使用WebClient C#将WEB转换为PDF时抛出(400)错误的请求 - ConvertAPI - WEB to PDF using WebClient C# throws (400) Bad Request 当我尝试使用 C# 在线使用 REST API 时收到错误请求状态代码 400 - I get a Bad request Status Code 400 when I am trying to consume a REST API online using C# WebClient UploadString在收到400错误请求时获取标头和正文 - WebClient UploadString get header and body when receive 400 bad request C#FCM:远程服务器返回错误:(400)错误的请求 - C# FCM : The remote server returned an error: (400) Bad Request 远程服务器返回错误:C# 代码中的 (400) 错误请求 - The remote server returned an error: (400) Bad Request in C# Code 远程服务器返回错误:(400)错误的请求C# - The remote server returned an error: (400) Bad Request C# WebClient UploadFile远程服务器返回错误:(400)错误的请求 - WebClient UploadFile The remote server returned an error: (400) Bad Request 我正在尝试使用 c# 从 Twitter api 获取请求令牌,但收到 400 个错误的请求错误 - I am trying to get request token from Twitter api using c#, but getting 400 bad request error 运行代码时收到 C# (400) 错误请求响应,而 URL 在 Web 浏览器上运行良好 - C# (400) Bad Request response received when running code whereas URL works well on web browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM