3

Long story short, I am sending an XML HTTP post request to an application server, and I am getting back a response, also in the form of XML HTTP.

I have a test site available to me which allows me to see what the server's actual response is, visually, in the form of XML, but I cannot access this XML from my C# code the way it is.

The XML coming back from the application server in my test case looks like this:

<Error><Message>StringErrorMessage</Message></Error>

However, I have had no luck accessing this basic XML to retrieve the value of "StringErrorMessage" for the creation of a detailed error report.

... More code above, all wrapped in a try{}...

_response = Serializer.DeserializeObject<T>(ObjectRequest.GetResponse().GetResponseStream());

        }
        catch (System.Net.WebException exceptionParameter)
        {
            var response = (HttpWebResponse)exceptionParameter.Response;
            string webExceptionStatus = exceptionParameter.Message;
            _exception = exceptionParameter;
            return false;
        }

I have consulted C# - Getting the response body from a 403 error

and

Get response body on 400 HTTP response in Android?

The first link's solution doesn't seem to give me access to the basic XML as part of any response object's properties. I am almost positive that there must be a byte[] in there somewhere (in the response, or in the exception object) that can be converted into a char[], which can be converted to a string, which can be converted to my XML body, but I have not been able to find it. The second link's solution is not exactly viable for me because I have to get the response body back in the form of XML, as it might not be an error, but an object that must be deserialized. This particular side of things, I cannot change.

Any advice would be very much appreciated. - Eli

EDIT: Just wanted to clarify that my basic code is working okay for non-error situations, and is deserializing the XML just fine. It's when my code encounters a HTTP 400 or an HTTP 500 error, where accessing the XML from the catch statement becomes a problem, because my code immediately throws an exception.

Community
  • 1
  • 1
justian17
  • 575
  • 1
  • 7
  • 17

1 Answers1

2

The body of a HTTP message (the XML in your case) can be retrieved with the GetResponseStream method of the HttpWebResponse object you have. And, since it's a stream, you can for instance read it with a StreamReader, like so:

HttpWebResponse myWebResponse; // Get this from whereever you want

Stream responseStream = myWebResponse.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);
string niceStringForYou = reader.ReadToEnd();

...and from that point on, you can do whatever to it.

If you're absolutely sure it's always gonna be XML you get back from the service, you can probably even use an XmlReader to get XML directly from the stream:

XmlReader foo = XmlReader.Create(responseStream);

Comment to edit: As long as you have the HttpWebResponse object, reading it's response stream (GetResponseStream()) should work. And as you point out in your own code, you can get the HttpWebResponse by looking at (HttpWebResponse)exceptionParameter.Response.

Arve Systad
  • 5,471
  • 1
  • 32
  • 58
  • 1
    Worked like a charm! Thanks, Arve - people like you make S.O. such a great site. – justian17 Mar 05 '14 at 20:10
  • Just to finish this off: I have not been able to make XmlReader work yet, sadly, but the first method described is working very well. Not sure what's coming back in the response at this point because when I have the following: XmlReader foo = XmlReader.Create(responseStream); string contents = foo.GetAttribute("Message"); contents stays null =*(. Maybe one day I shall unravel this mystery... – justian17 Mar 05 '14 at 20:27