views:

3516

answers:

3

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:

http://www.flickr.com/services/api/upload.api.html

+1  A: 

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 :-)

Ron Klein
Sadly, this is what I had to do.
Jesse Weigert
+1  A: 

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.

Wil P
That would work if I would just uploading the file. However, I need to include a bunch of other form variables along with it.
Jesse Weigert
+1  A: 

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.

Ryan Barton