Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContent boundary double quotes

I have this code sample that was posted as an answer to another question (Send a file via HTTP POST with C#). It works fine except for one issue. It surrounds the boundary in the HTTP header with double quotes:

multipart/form-data; boundary="04982073-787d-414b-a0d2-8e8a1137e145"

This is choking the webservice that I'm trying to call. Browsers don't have those double quotes. I need some way to tell .NET to leave them off.

private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)
{
    HttpContent stringContent = new StringContent(paramString);
    HttpContent fileStreamContent = new StreamContent(paramFileStream);
    HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
    using (var client = new HttpClient())
    using (var formData = new MultipartFormDataContent())
    {
        formData.Add(stringContent, "param1", "param1");
        formData.Add(fileStreamContent, "file1", "file1");
        formData.Add(bytesContent, "file2", "file2");
        var response = client.PostAsync(actionUrl, formData).Result;
        if (!response.IsSuccessStatusCode)
        {
            return null;
        }
        return response.Content.ReadAsStreamAsync().Result;
    }
}
like image 731
jtaylor___ Avatar asked Jun 18 '15 22:06

jtaylor___


2 Answers

You can remove the quotes from the boundary by using the following code:

var boundary = formData.Headers.ContentType.Parameters.First(o => o.Name == "boundary"); 
boundary.Value = boundary.Value.Replace("\"", String.Empty);    
like image 112
Luis Perez Avatar answered Nov 11 '22 21:11

Luis Perez


Expanding on the dicsussion in a similar issue in the dotnet runtime repo. Choosing to not have quotes involves two steps

  1. choose your own non-quoted boundary
  2. remove auto-generated ContentType header with one with no quotes
private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)
{
    HttpContent stringContent = new StringContent(paramString);
    HttpContent fileStreamContent = new StreamContent(paramFileStream);
    HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
    //Set your own boundary, otherwise the internal boundary between multiple parts remains with quotes
    string boundaryDelimiter = String.Format("----------{0}", DateTime.Now.Ticks.ToString("x"));
    using (var client = new HttpClient())
    using (var formData = new MultipartFormDataContent(boundaryDelimiter))
    {
        formData.Add(stringContent, "param1", "param1");
        formData.Add(fileStreamContent, "file1", "file1");
        formData.Add(bytesContent, "file2", "file2");
        //remove quotes from ContentType Header
        _multiPartContent.Headers.Remove("Content-Type");
        _multiPartContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundaryDelimiter);

        var response = client.PostAsync(actionUrl, formData).Result;
        if (!response.IsSuccessStatusCode)
        {
            return null;
        }
        return response.Content.ReadAsStreamAsync().Result;
    }
}
like image 2
OzBob Avatar answered Nov 11 '22 22:11

OzBob