c# - Posting attached file to a web service via httpwebrequest -
i trying post xml file web service.
this current code
var tempfilelocation = @"c:\temp\"; xmldocument xdoc = new xmldocument(); xdoc.loadxml(item); xdoc.save(tempfilelocation + "\\pcf.xml"); var url = "https://siteaddress/xml.aspx"; utf8encoding encoding = new utf8encoding(); byte[] byte1 = encoding.getbytes(xdoc.innerxml); httpwebrequest submissionrequest = (httpwebrequest)webrequest.create(url); submissionrequest.keepalive = true; submissionrequest.method = "post"; submissionrequest.contenttype = "multipart/form-data"; submissionrequest.contentlength = byte1.length; submissionrequest.timeout = 10000; submissionrequest.headers.add("accept-encoding", "gzip,deflate"); stream requeststream = submissionrequest.getrequeststream(); requeststream.write(byte1, 0, byte1.length); requeststream.close(); xmldocument responsexmldocument = new xmldocument(); httpwebresponse submissionacknowledgement = (httpwebresponse)submissionrequest.getresponse(); if (submissionacknowledgement.statuscode == httpstatuscode.ok) { responsexmldocument.load(submissionacknowledgement.getresponsestream()); }
i receive following message.
bad request.form[pcfxml] - should have been attached post
how should post xml file attachment?
seems boundary missing. need set boundary multipart upload...
boundary = getboundary(); request.contenttype = "multipart/form-data; boundary=" + boundary;
and boundary should below. please note whenever write byte request stream, please make sure have mentioned boundary.
private static string getboundary() { return "--------------------" + datetime.now.ticks.tostring("x"); }
Comments
Post a Comment