I'm working on an ASP.NET MVC application. There's a service which I need to send RESTful requests to. In this particular case, I have to use a POST to send XML to the service. We are not using WCF.
The XML is in an XElement
object. The original XML includes character encoding information. I want to keep the character encoding unchanged when I write the XML to the service.
I can get a reference to the request stream using code like this:
HttpWebRequest req = (HttpWebRequest) WebRequest.Create( url );
req.Method = "POST";
req.Timeout = 30000;
Stream requestStream = req.GetRequestStream();
I've written this code:
using ( XmlWriter writer = new XmlTextWriter( requestStream, Encoding.UTF8 ) ) {
xml.WriteTo( writer );
}
But this may change the encoding of the XML. It's important that the encoding doesn't change. I can't seem to find a property or method that returns the encoding of the XElement.
How do I write the XElement
to the requestStream
and preserve the existing character encoding?