We help IT Professionals succeed at work.

How can I get an XML response from a request to a remote server?

YPU
YPU asked
on
We are setting up recurring billing on our web app and we are going to be using Cheddar Getter Here's the request dictionary.

We are going to use an existing wrapper written in C# (see it here here)

The problem is, the way the original wrapper was written, we are receiving an http response from the server, and not the xml that the server sends back.

Here is an example method that I've been trying to get to return the xml from Cheddar Getter:

        private static string postRequest(string urlBase, string urlPath, string postParams)
        {
            string result = "";

            try
            {
                HttpWebRequest request = WebRequest.Create(urlBase + urlPath) as HttpWebRequest;

                //Add authentication
                request.Credentials = new NetworkCredential(_Username, _Password);

                //make into a post
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";

                byte[] bytes = Encoding.UTF8.GetBytes(postParams);
                request.ContentLength = bytes.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);

                    using (WebResponse response = request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

Open in new window


Using the above method with incomplete data (we're trying to get the gooey details of an error returned from Cheddar Getter), as soon as you call

using (WebResponse response = request.GetResponse())

Open in new window


a server error is returned (412). This is not desirable, as Cheddar Getter provides XML output with more detailed information about the error (missing name, invalid CC, etc).

If anyone could point me in the right direction, that would be awesome!
Comment
Watch Question

Brett CrawleyZone Leader

Commented:
The status code means that this is a problem not so much related to the data you are sending but related to a conditional element in the request headers.

412 - Precondition Failed

I would suggest using a packet sniffer like wireshark to view exactly what is being sent as the request.

You may find that not all the variables in the rest path http://hostname/pathelement/{1}/etc such as the {1} in this case are not being substituted correctly
Commented:
I was able to catch the XML that was returned thusly:

            catch (WebException wex)
            {
                HttpWebResponse response = wex.Response as HttpWebResponse;

                XDocument xDoc = XDocument.Load(response.GetResponseStream());
            }

Open in new window

YPU

Author

Commented:
This is exactly what I was looking for.