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;
}
}
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:
Hope this helps someone out there.