接住一個WebFaultException的細節我有拋出WebFaultException在WCF休息
try
{
...
}
catch (InvalidPasswordException)
{
throw new WebFaultException<String>
("This is a bad password",
HttpStatusCode.Unauthorized);
}
到目前爲止一切順利的服務器。但是,當這個WebFault被另一個C#項目(客戶端)捕獲時,我不知道如何獲取細節。
try
{
HttpWebRequest httpWebRequest = WebRequest.Create(uri) as HttpWebRequest;
httpWebRequest.Method = verb;
httpWebRequest.ContentType = "text/xml";
httpWebRequest.ContentLength = serializedPayload.Length;
using (StreamWriter streamOut = new StreamWriter(httpWebRequest.GetRequestStream(), Encoding.ASCII))
{
streamOut.Write(serializedPayload);
streamOut.Close();
}
// error here on GetResponseStream
using (StreamReader streamIn = new StreamReader(httpWebRequest.GetResponse().GetResponseStream()))
{
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
return strResponse;
}
}
catch (Exception e)
{
// The exception is of type WebException with unauthorized but don't know where the details are
}
此代碼捕獲WebException,我找不到詳細信息,只是未經授權。
感謝
更新1:當我做小提琴手請求的響應體是細節,但作爲例外的是響應體之前拋出的是讀過那麼它沒有顯示出來。所以問題是我如何閱讀響應主體DESPITE一個非200被拋出。
提琴手原始響應:
HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 12 Oct 2010 22:42:31 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 100
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Email/Password Mismatch</string>
什麼是你的客戶?它是一個Web應用程序,或者一個Silverlight應用程序? – 2010-10-12 23:18:02
C#.NET 4.0,windows窗體 – 2010-10-12 23:28:35