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;
}
}
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);
Expanding on the dicsussion in a similar issue in the dotnet runtime repo. Choosing to not have quotes involves two steps
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;
}
}
This is an old question but I had the same problem and solved the same way pointed out by @Luis Perez in his answer but using that has the drawback that MultipartContent stops calculating content size so this was my solution:
var multipartContent = new MultipartFormDataContent();
var boundaryParameter = multipartContent.Headers.ContentType
.Parameters.Single(p => p.Name == "boundary");
boundaryParameter.Value = boundaryParameter.Value.Replace("\"", "");
// --- Put the rest of content here
var size = (await multipartContent.ReadAsStreamAsync()).Length;
multipartContent.Headers.ContentLength = size;
Hope this helps someone out there.