How do I POST XML generated to a URL in C#

Posted by user2922687 on Programmers See other posts from Programmers or by user2922687
Published on 2013-10-27T15:24:05Z Indexed on 2013/10/27 15:59 UTC
Read the original article Hit count: 193

Filed under:

Hi guys an new to C# and i need help, am trying to send XML generated to a URL, I keep getting error with HttpWebResponse. This is my code.

 //POST to URL 

            var httpRequest = (HttpWebRequest)WebRequest.Create("http://xxx.xxx.xxx.xxx:8000");
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.ProtocolVersion = HttpVersion.Version11;


            //Set appropriate headers 

            var xmlWriterSettings = new XmlWriterSettings
            {
                NewLineHandling = NewLineHandling.None,
                Encoding = Encoding.ASCII
            };

            using (var requestStream = httpRequest.GetRequestStream())
            {
                xmlDoc.Save(requestStream);

            }

            using (var response = (HttpWebResponse)httpRequest.GetResponse())
            using (var responseStream = response.GetResponseStream())
            {
                // Response Code to see if the request was successful
                var responseXml = new XmlDocument();
                responseXml.Load(responseStream);
                using (var repp = XmlWriter.Create("response.xml"))
                {
                    responseXml.Save(repp);
                }
            }

© Programmers or respective owner

Related posts about c#