Multipart Form Posts in .NET

Recently I decided I wanted to develop a small, lightweight Windows client to use for Baconfile, Leah Culver‘s latest side project for easily uploading files to Amazon’s S3 service. Chris Wanstrath made a nice little Mac client and I thought Windows users might enjoy the same type of functionality.

After deciding on how I wanted the app to look and feel, I got to work. At least, I tried to. Strait away I ran into issues in both trying to write a Windows Explorer extension in .NET and POSTing a multi-part form POST. I don’t want to devolve into too much of a rant but it IS 2009 and we STILL can’t write simple Explorer extensions using managed code which just reinforces my feeling that .NET programmers are second-class Windows citizens. Version 4.0 is around the corner and I still have to import kernel32.dll to perform some basic OS functions.

The other problem, POSTing a multi-part form POST turned out to be a little bit more surmountable an issue. The goal was to use the Baconfile API to upload a file. In other languages such as Ruby, constructing a multi-part form post is actually quite simple. In C# it is not. Between HttpWebRequest and the WebClient Upload method, there was no way to create a custom post whereby I added some arbitrary number of POST parameters.

After awhile I found Brian Grinstead‘s post about how he had constructed his own class that enabled users to create their own custom-formed multi-part form POST. Considering how often a function like this is used, especially on public APIs, I was as surprised as he was that this kind of functionality wasn’t in the .NET framework itself. His example class worked flawlessly.

I needed to add some tweaks such as basic http auth and so I created a new GitHub project to support the work.

I plan to add more functions such as a cookie container, ActiveDirectory auth, and whatever else seems logical. If I’ve missed something and there really IS an easy way to do this using out-of-the-box classes in the .NET framework, please let me know.