在WebException中,我看不到GetResponse的主體。這是我在C#中的代碼:WebException如何獲得整體響應與身體?
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? – 2012-08-06 13:06:01
爲了在重新拋出的異常中保存堆棧跟蹤,不要使用'throw ex;'而只是'throw;'(在默認情況下)。另外(如果需要的話),我會把原來的WebException放入你的自定義異常的InnerException(通過適當的構造函數)。 – user1713059 2014-04-04 17:38:27