Hey,
I have a server which is throwing WebFaultException
try
{
...
}
catch (InvalidPasswordException)
{
throw new WebFaultException<String>
("This is a bad password",
HttpStatusCode.Unauthorized);
}
So far so good. However when this WebFault is caught in another C# project (the client) I don't know how to get the details.
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
}
This code catches a WebException which I can't find the details for, just the unauthorized.
Thanks
UPDATE 1: When I do the request in fiddler the response body is the detail but as that exception is thrown before the response body is ever read then it isn't showing up. So the question is how do I read the response body DESPITE a non 200 being thrown.
Fiddler Raw Response:
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>