0

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

har07
  • 88,338
  • 12
  • 84
  • 137
MarsOne
  • 2,155
  • 5
  • 29
  • 53

3 Answers3

0

Get rid of sout.Flush();, It's not necessary.

javisrk
  • 582
  • 4
  • 19
0

It seems the error message is clear , it asks for multipart boundary. Content-type: multipart/mixed; boundary="boundary"

What is the boundary parameter in an HTTP multi-part (POST) Request?

Community
  • 1
  • 1
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
0

I know this is an old thread but I wanted to share my solution with the community as I was plagued with similar issues doing a multipart request this past week. I couldn't use RestSharp as it doesn't support content-type other thant form-data and I needed "related". I also needed to base in the base64 string of a pdf with json metadata. Below is a sample function I wrote to accomplish this using HttpClient and HttpRequestMessage. I think you could take this structure and easily swap out headers for what you are trying to do.

public static void PostBase64PdfHttpClient(string recordID, string docName, string pdfB64)
    {
        string url = $"baseURL";
        HttpClient client = new HttpClient();
        var myBoundary = "------------ThIs_Is_tHe_bouNdaRY_";
        string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes($"UN:PW"));
        client.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}");

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{url}/api-endpoint");
        request.Headers.Date = DateTime.UtcNow;
        request.Headers.Add("Accept", "application/json; charset=utf-8");
        MultipartContent mpContent = new MultipartContent("related", myBoundary);
        mpContent.Headers.TryAddWithoutValidation("Content-Type", $"multipart/related; boundary={myBoundary}");

        dynamic jObj = new Newtonsoft.Json.Linq.JObject(); jObj.ID = recordID; jObj.Name = docName;
        var jsonSerializeSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
        var json = JsonConvert.SerializeObject(jObj, jsonSerializeSettings);

        mpContent.Add(new StringContent(json, Encoding.UTF8, "application/json"));
        mpContent.Add(new StringContent(pdfB64, Encoding.UTF8, "application/pdf"));

        request.Content = mpContent;

        HttpResponseMessage response = client.SendAsync(request).Result;
    }