Reputation: 7335
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());
Upvotes: 7
Views: 8740
Reputation: 136717
There's nothing fundamentally wrong with what you're doing, but you need to flush/close the request stream writer. This can be easily done with the using
construct as disposing the writer also flushes it:
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
{
writer.WriteLine(xml.ToString());
}
You then need to call GetResponse
to actually execute the request:
req.GetResponse()
(Note that the HttpWebResponse
returned from this is also disposable, so don't forget to dispose that too.)
Upvotes: 5
Reputation: 36679
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)));
}
Upvotes: 6