c# - Issues passing an Xml file to a method in console application -
i working on c# console application making http post request
web api using xml file , i'm kind of new xml
, web services figured out following code request failed pass xml data
method
static void main(string[] args) { string desturl=@"https://xyz.abcgroup.com/abcapi/"; program p = new program(); system.console.writeline(p.webrequestpostdata(desturl, @"c:\applications\testservice\fixmlsub.xml")); } public string webrequestpostdata(string url, string postdata) { system.net.webrequest req = system.net.webrequest.create(url); req.contenttype = "text/xml"; req.method = "post"; byte[] bytes = system.text.encoding.ascii.getbytes(postdata); req.contentlength = bytes.length; using (stream os = req.getrequeststream()) { os.write(bytes, 0, bytes.length); } using (system.net.webresponse resp = req.getresponse()) { if (resp == null) return null; using (system.io.streamreader sr = new system.io.streamreader(resp.getresponsestream())) { return sr.readtoend().trim(); } } }
for obvious reasons above code throws 404 error think not passing xml data
properly
may know how can fix this?
you're not posting xml, posting string c:\applications\testservice\fixmlsub.xml
change method call from:
system.console.writeline(p.webrequestpostdata(desturl, @"c:\applications\testservice\fixmlsub.xml"));
to
var xml = xelement.load(@"c:\applications\testservice\fixmlsub.xml"); system.console.writeline(p.webrequestpostdata(desturl, xml.tostring(saveoptions.disableformatting));
if trying learn post / receive, go it. there open source libraries tested use if want use them.
the non-free version of servicestack. , older free-version. think older free version great. i've never tried newer one. deal objects, employee , pass library , translation xml or whatever web-service wants.
you can post whole strings if want. have great extension methods too.
Comments
Post a Comment