WebException如何获取带有正文的完整响应?

122

在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中看到消息文本呢?谢谢。


你尝试过使用(HttpWebResponse)we.Response;这个语句吗?其中'we'是你捕获的WebException。 - Justin Harvey
2
为了保留在重新抛出的异常中的堆栈跟踪,请勿使用 throw ex;,而是使用简单的 throw;(在默认情况下)。此外(如果需要),我会通过适当的构造函数将原始WebException放入自定义异常的InnerException中。 - user1713059
4个回答

227
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;

9
如果有人不熟悉JsonConvert,你需要从Nuget包管理器中获取Newtonsoft.Json。 - Kyle
请更新答案,加上Kyle的解释,因为Newtonsoft.Json是可选的。 - Jeroen
3
请注意,您需要将此代码放置在Try-Catch代码块的Catch回退子句中,在其中请求应该去。对于关注的读者和@iwtu来说,这种情况显然,但全面详尽的答案可以为初学者阅读此答案带来真正的差异;) - Jeroen
2
StreamReader 实现了 IDisposable 接口,所以将其包装在 using 语句中是最佳实践吗?快速查看一下 StreamReader 的 Dispose 方法会发现它在其中进行了一些重要的清理工作。 - sammy34
@sammy34 不用担心,因为在这种情况下没有非托管的代码/数据,垃圾回收器可以轻松处理...(但是始终使用using是一个好习惯) - L.B
我想指出的是,这只会获取响应正文。如果出现SSL/TLS错误,则没有响应正文。您需要在表达式中使用空值条件(“?.”)或两个。 - James

46
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();
}

7

这篇文章是在现有答案的基础上进一步完善。我编写了一个方法来处理抛出/重新抛出异常的详细信息,并包含响应主体:

以下是我的代码(Client.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;
}

您可以像 OP 显示的那样在 catch 子句中使用它:
//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;
}

2

我没有看到任何使用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);
}
警告:如果您尝试两次调用此方法,将会抛出一个异常,指出流已被处理。这个扩展方法只适合快速显示错误并继续执行。如果您需要更多的逻辑,您可能需要编写自己的方法。

不要使用string.Empty,如何使用异常的Message属性呢?同时要检查GetResponseStream()是否为null。 - bvj
1
答案可以修改以适应特定的问题。至于空值检查,我还没有遇到过空值的情况,但是确实最好进行空值检查。 - GLJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接