【发布时间】:2011-04-24 14:11:21
【问题描述】:
我有一个服务器正在抛出 WebFaultException
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:当我在提琴手中执行请求时,响应正文是详细信息,但由于在读取响应正文之前引发了异常,因此它不会显示。所以问题是我如何阅读响应正文尽管抛出了非 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 应用程序?