Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post XML to a URL

Tags:

c#

.net

xml

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.

like image 769
OBL Avatar asked Feb 25 '13 23:02

OBL


People also ask

How do I send an XML file?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.

Can I send XML in REST API?

The REST API Client Service currently accepts only JSON input when making REST API Create, Read, Update, or Delete requests. It is nevertheless possible to use XML input when making REST API requests.

How do I post XML data with curl?

To post XML using Curl, you need to pass XML data to Curl with the -d command line parameter and specify the data type in the body of the POST request message using the -H Content-Type: application/xml command line parameter.

Can XML be used for transmitting data?

General applications: XML provides a standard method to access information, making it easier for applications and devices of all kinds to use, store, transmit, and display data.


1 Answers

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

    using (var writer = new StreamWriter(req.GetRequestStream()))
    {
        writer.Write(xml);
    }
like image 53
Al W Avatar answered Oct 26 '22 10:10

Al W