Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i call Rest Webserivce with a post method and send xml data in C#

Tags:

rest

c#

post

xml

What i am trying to do here is make post request to Rest webserivce with xml data.

this is what i have right now but i am not sure how to pass my xml data

            XElement xml = new XElement("MatchedOptions",
               from m in _matchedOptionsList
               select new XElement("Listing",
                       new XElement("DomainID", _trafficCopInputs.DomainID),
                       new XElement("AdSource", _trafficCopInputs.AdSource),
                       new XElement("Campaign", _trafficCopInputs.Campaign),
                       new XElement("AdGroup", _trafficCopInputs.AdGroup),
                       new XElement("RedirectURL", m.RedirectPath),
                       new XElement("FunnelKeyword", m.FunnelKeyword)));

            HttpWebRequest req = WebRequest.Create("http://something.com/")
                 as HttpWebRequest;


            req.Method = "POST";
            req.ContentType = "text/xml";
            req.ContentLength = 0;
            StreamWriter writer = new StreamWriter(req.GetRequestStream());
            writer.WriteLine(xml.ToString());
like image 752
devforall Avatar asked Sep 22 '09 13:09

devforall


1 Answers

I use the WebClient class:

WebClient webClient = new WebClient();
using (webClient)
{
   requestInterceptor.OnRequest(webClient);
   var enc = new ASCIIEncoding();
   return enc.GetString(webClient.UploadData(uri, enc.GetBytes(dataAsString)));
}
like image 110
Grzenio Avatar answered Sep 24 '22 00:09

Grzenio