Trouble with MultiPart/Form-Data as a web request



I have a HTML POST request which i need to replicate in c#


The HTML is something like



<FORM action="http://RemoteServerURL" enctype="multipart/form-data" method=POST>
<TEXTAREA id="TextAreaXML" name="xmlmsg" rows="20" cols="100"> </TEXTAREA>
<button type="submit">Send</button>
</form>


The TextArea Expects an inout which is as below



<?xml version="1.0" encoding="utf-8"?>
<OnlineCheck>
<Header>
<BuyerAccountId>XXXXXX</BuyerAccountId>
<AuthCode>XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</AuthCode>
<Type>STOCK</Type>
</Header>
<Item line="1">
<ManufacturerItemIdentifier />
<DistributorItemIdentifier>3109750</DistributorItemIdentifier>
<Quantity>7</Quantity>
</Item>
</OnlineCheck>


This Part works Fine.


Now i tried to replicate it in c# like below.



WebRequest req = WebRequest.Create("http://RemoteServerURL");
string xmlmsg = "<?xml version=" + '"' + "1.0" + '"' + " encoding==" + '"' + "utf-8" + '"' + "?><OnlineCheck><Header><BuyerAccountId>XXXXXX</BuyerAccountId><AuthCode>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX</AuthCode><Type>STOCK</Type></Header><Item line=" + '"' + "1" + '"' + "><ManufacturerItemIdentifier /><DistributorItemIdentifier>3109750</DistributorItemIdentifier><Quantity>7</Quantity></Item></OnlineCheck>";

byte[] send = Encoding.Default.GetBytes(xmlmsg);
req.Method = "POST";
req.ContentType = "multipart/form-data";
req.ContentLength = send.Length;

Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();

WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();


This fails and I get the message



"The request was rejected because no multipart boundary was found XML request"



. So i know the request to the server is working. But the input or something is going wrong. Please Help


No comments:

Post a Comment