I have a C/S application. I implemented the server with Asp.Net MVC 4.0, and the client runs on .Net 4.5.
I have a Controller Action in the server side looks like th
After debugging with Fiddler, comparing raw http message with WinMerge, I found the differences between Firefox and my program:
Firefox (removed some headers to make things simpe):
POST http://localhost:53400/Input/Upload HTTP/1.1
Host: localhost:53400
Content-Type: multipart/form-data; boundary=---------------------------1590871622043
Content-Length: ****
-----------------------------1590871622043
Content-Disposition: form-data; name="arg1"
abc
-----------------------------1590871622043
Content-Disposition: form-data; name="arg2"
3
-----------------------------1590871622043
Content-Disposition: form-data; name="uploadfile"; filename="wave.wav"
Content-Type: audio/wav
//file data here
-----------------------------1590871622043--
My Program with MultipartFormDataContent
:
POST http://localhost:53400/Input/Save HTTP/1.1
Content-Type: multipart/form-data; boundary="caac5ea7-8ab4-4682-be40-ecb3ddf3e70a"
Host: localhost:53400
Content-Length: ****
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a
Content-Disposition: form-data; name=arg1
abc
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a
Content-Disposition: form-data; name=arg2
3
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a
Content-Disposition: form-data; name=uploadfile; filename=wave.wav; filename*=utf-8''wave.wav
//file data here
--caac5ea7-8ab4-4682-be40-ecb3ddf3e70a--
The last thing I would notice is that in these Content-Disposition
lines, Firefox quotes all values, but my program does not. One could easily assume that it would not matter, but in the end, I found that is the exact cause.
Now that I know the reason, here comes the code that works, as easy as quoting the names:
var multipart = new MultipartFormDataContent();
multipart.Add(new StringContent("abc"), '"' + "arg1" + '"');
multipart.Add(new StringContent("3"), '"' + "arg2" + '"');
// byte[] fileData;
multipart.Add(new ByteArrayContent(fileData), '"' + "uploadfile"+ '"', '"' + "wave.wav" + '"');
//HttpClient http; string url;
var response = await http.PostAsync(url, multipart);
response.EnsureSuccessStatusCode();
//...