0

I am writing a small console application in C# to send a request to REST API and receive the response in XML. below is my code

 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(reader.ReadToEnd());
                Console.Write(xmlDoc);

            } 

I have checked using the fiddler and i am getting 200 response code and result. but while doing xmlDoc.Load(reader.ReadToEnd()); i am getting error.

Can any one help me to solve this? or is there any other way to do this.

I want to write the response XML result into file.

Thanks in advance

2
  • What error are you getting? Commented Sep 11, 2014 at 9:25
  • Any clue as to waht error you are getting? Commented Sep 11, 2014 at 9:25

1 Answer 1

1

Try this:

        string response;
        try
        {
            using (StreamReader streamIn = new StreamReader((webRequest.GetResponse()).GetResponseStream()))
            {
                response = streamIn.ReadToEnd();
                streamIn.Close();
            }
        }finally
        {webRequest.Abort();}

       XDocument xDoc = XDocument.Parse(response);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.