接住一个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