OBL
OBL

Reputation: 1357

Post XML to a URL

I am trying to post an XMLDocument to an URL. This is what I have so far:

   var uri = System.Configuration.ConfigurationManager.AppSettings["Url"];
   var template = System.Configuration.ConfigurationManager.AppSettings["Template"];
   XmlDocument reqTemplateXml = new XmlDocument();
   reqTemplateXml.Load(template);

   reqTemplateXml.SelectSingleNode("appInfo/appNumber").InnerText = x; 
   reqTemplateXml.SelectSingleNode("appInfo/coappNumber").InnerText = y;

   WebRequest req = null;
   WebResponse rsp = null;
   req = WebRequest.Create(uri);
   req.Method = "POST";
   req.ContentType = "text/xml";
   rsp = req.GetResponse();

What I am trying to figure out is how to load this XmlDocument to the WebRequest object so that it can be posted to that URL.

Upvotes: 9

Views: 6173

Answers (1)

Al W
Al W

Reputation: 7713

you need to write to the RequestStream before calling req.GetResponse() like this.

    using (var writer = new StreamWriter(req.GetRequestStream()))
    {
        writer.Write(xml);
    }

Upvotes: 14

Related Questions