I am trying to post multipart data using System.Net.Http.HttpClient but when I am instanciating my content I am getting this exception:
A first chance exception of type 'System.ArgumentException' occurred in System.Net.Http.DLL
An exception of type 'System.ArgumentException' occurred in System.Net.Http.DLL and wasn't handled before a managed/native boundary
e: System.ArgumentException: The format of value '---###---' is invalid.
Parameter name: boundary
at System.Net.Http.MultipartContent.ValidateBoundary(String boundary)
at System.Net.Http.MultipartContent..ctor(String subtype, String boundary)
at System.Net.Http.MultipartFormDataContent..ctor(String boundary)
at RestClientUploadFoto.MultipartStackOverflow.<postMultipart>d__2.MoveNext()
- Why does it happen?
here is the method in where I am getting the exception:
public async Task postMultipart()
{
var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
// boundary
String boundary = "---###---"; // should never occur in your data
// This is the postdata
MultipartFormDataContent content = new MultipartFormDataContent(boundary);
content.Add(new StringContent("12", Encoding.UTF8), "userId");
content.Add(new StringContent("78", Encoding.UTF8), "noOfAttendees");
content.Add(new StringContent("chennai", Encoding.UTF8), "locationName");
content.Add(new StringContent("32.56", Encoding.UTF8), "longitude");
content.Add(new StringContent("32.56", Encoding.UTF8), "latitude");
Console.Write(content);
// upload the file sending the form info and ensure a result.
// it will throw an exception if the service doesn't return a valid successful status code
await client.PostAsync(fileUploadUrl, content)
.ContinueWith((postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
}
---###---is an invalid boundary. Have you tried using a different boundary string?