What is the easiest way to submit an HTTP POST request with a multipart/form-data content type from C#? There has to be a better way than building my own request.
The reason I'm asking is to upload photos to Flickr using this api:
What is the easiest way to submit an HTTP POST request with a multipart/form-data content type from C#? There has to be a better way than building my own request.
The reason I'm asking is to upload photos to Flickr using this api:
The System.Net.WebClient class may be what you are looking for. Check the documentation for WebClient.UploadFile, it should allow you to upload a file to a specified resource via one of the UploadFile overloads. I think this is the method you are looking to use to post the data...
It can be used like.... note this is just sample code not tested...
WebClient webClient = new WebClient();
webClient.UploadFile("http://www.url.com/ReceiveUploadedFile.aspx", "POST", @"c:\myfile.txt");
Here is the MSDN reference if you are interested.
http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile.aspx
Hope this helps.
I've had success with the code posted at aspnetupload.com. I ended up making my own version of their UploadHelper library which is compatible with the Compact Framework. Works well, seems to do exactly what you require.
First of all, there's nothing wrong with pure manual implementation of the HTTP commands using the .Net framework. Do keep in mind that it's a framework, and it is supposed to be pretty generic.
Secondly, I think you can try searching for a browser implementation in .Net. I saw this one, perhaps it covers the issue you asked about. Or you can just search for "C# http put get post request". One of the results leads to a non-free library that may be helpful (Chilkat Http)
If you happen to write your own framework of HTTP commands on top of .Net - I think we can all enjoy it if you share it :-)
I normally find Fiddler to be the best tool for that job. Very ease to create requests and it even generates some of the headers for you.
Fiddler - How to create a request
I have not tried this myself, but there seems to be a built-in way in C# for this (although not a very known one apparently...):
Try it out and let me know how it goes.
Cheers!
If you are using .NET 4.5 use this:
Otherwise Based on Ryan's answer, I downloaded the library and tweaked it a bit.