【发布时间】:2012-08-03 10:58:27
【问题描述】:
在 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;你捕获的 WebException 在哪里“我们”?
-
要在重新抛出的异常中保留堆栈跟踪,不要使用
throw ex;,而只需使用throw;(在默认情况下)。另外(如果需要)我会将原始 WebException 放在您的自定义异常的 InnerException 中(通过适当的构造函数)。
标签: c# webexception