WebException 如何用一个正文获得完整的响应
本文关键字:响应 何用一 正文 WebException | 更新日期: 2024-10-26 01:01:25
在WebException中,我看不到GetResponse的正文。这是我在 C# 中的代码:
try {
return GetResponse(url + "." + ext.ToString(), method, headers, bodyParams);
} catch (WebException ex) {
switch (ex.Status) {
case WebExceptionStatus.ConnectFailure:
throw new ConnectionException();
case WebExceptionStatus.Timeout:
throw new RequestTimeRanOutException();
case WebExceptionStatus.NameResolutionFailure:
throw new ConnectionException();
case WebExceptionStatus.ProtocolError:
if (ex.Message == "The remote server returned an error: (401) unauthorized.") {
throw new CredentialsOrPortalException();
}
throw new ProtocolErrorExecption();
default:
throw;
}
我看到标题,但我看不到正文。这是 Wireshark 为请求输出的:
POST /api/1.0/authentication.json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Host: nbm21tm1.teamlab.com
Content-Length: 49
Connection: Keep-Alive
userName=XXX&password=YYYHTTP/1.1 500 Server error
Cache-Control: private, max-age=0
Content-Length: 106
Content-Type: application/json; charset=UTF-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
Date: Mon, 06 Aug 2012 12:49:41 GMT
Connection: close
{"count":0,"startIndex":0,"status":1,"statusCode":500,"error":{"message":"Invalid username or password."}}
是否可以以某种方式在WebException中看到消息文本?谢谢。
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;
try {
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
string content = client.DownloadString("https://sandiegodata.atlassian.net/wiki/pages/doaddcomment.action?pageId=524365");
Console.WriteLine(content);
Console.ReadKey();
} catch (WebException ex) {
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
Console.WriteLine(resp);
Console.ReadKey();
}
这只会改进现有的答案。我编写了一个方法,该方法使用增强的消息来处理抛出/重新抛出的细节,其中包括响应正文:
这是我的代码(在客户端.cs中):
/// <summary>
/// Tries to rethrow the WebException with the data from the body included, if possible.
/// Otherwise just rethrows the original message.
/// </summary>
/// <param name="wex">The web exception.</param>
/// <exception cref="WebException"></exception>
/// <remarks>
/// By default, on protocol errors, the body is not included in web exceptions.
/// This solutions includes potentially relevant information for resolving the
/// issue.
/// </remarks>
private void ThrowWithBody(WebException wex) {
if (wex.Status == WebExceptionStatus.ProtocolError) {
string responseBody;
try {
//Get the message body for rethrow with body included
responseBody = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
} catch (Exception) {
//In case of failure to get the body just rethrow the original web exception.
throw wex;
}
//include the body in the message
throw new WebException(wex.Message + $" Response body: '{responseBody}'", wex, wex.Status, wex.Response);
}
//In case of non-protocol errors no body is available anyway, so just rethrow the original web exception.
throw wex;
}
您可以在 catch 子句中使用它,就像 OP 所示一样:
//Execute Request, catch the exception to eventually get the body
try {
//GetResponse....
}
} catch (WebException wex) {
if (wex.Status == WebExceptionStatus.ProtocolError) {
ThrowWithBody(wex);
}
//otherwise rethrow anyway
throw;
}
我没有看到任何带有using
语句的答案,也没有看到async
的任何用途。
public static class WebExceptionExtensions
{
public static string GetResponseBody(this WebException webException)
{
if (webException.Status == WebExceptionStatus.ProtocolError)
{
try
{
using (var stream = webException.Response.GetResponseStream())
{
if (stream is null)
return string.Empty; // or webException.Message
using (var reader = new StreamReader(stream))
{
string msg = reader.ReadToEnd();
if (string.IsNullOrEmpty(msg) && webException.Response is HttpWebResponse response)
msg = $"{response.StatusDescription} ({(int)response.StatusCode})"; // provide some error message if not found
return msg;
}
}
}
catch (WebException) // we tried
{
return string.Empty; // or webException.Message
}
}
else
{
return string.Empty; // or webException.Message
}
}
public static async Task<string> GetResponseBodyAsync(this WebException webException)
{
if (webException.Status == WebExceptionStatus.ProtocolError)
{
try
{
using (var stream = webException.Response.GetResponseStream())
{
if (stream is null)
return string.Empty; // or webException.Message
using (var reader = new StreamReader(stream))
{
string msg = await reader.ReadToEndAsync();
if (string.IsNullOrEmpty(msg) && webException.Response is HttpWebResponse response)
msg = $"{response.StatusDescription} ((int){response.StatusCode})"; // provide some error message if not found
return msg;
}
}
}
catch (WebException) // we tried
{
return string.Empty; // or webException.Message
}
}
else
{
return string.Empty; // or webException.Message
}
}
}
现在,每当我们捕获WebExceptions时,都很容易获得响应正文。
try
{
// Do work here...
}
catch (WebException we)
{
Console.WriteLine(we.GetResponseBody()); // synchronous
Console.WriteLine(await we.GetResponseBodyAsync()); // or asynchronous
}
catch (Exception e)
{
throw new Exception("Unexpected error occured", e);
}
请注意,如果尝试调用此方法两次,将出现流已被释放的异常。这种扩展方法实际上只适用于快速显示错误并继续前进。如果需要扩展逻辑,则可能需要创建自己的方法。