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

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!
YPUAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brett CrawleyZone LeaderCommented:
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
0
YPUAuthor 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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YPUAuthor Commented:
This is exactly what I was looking for.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.