MultipartForm Uploader

Dot Net Bugs and Fun Creating a work around....

MultipartForm Uploader addresses a bug in .NET and also adds the ability to include data fields with the upload.  During a recent project with Panasonic I discovered a bug in the way the C# class WebClient handles the upload of a file in the method WebClient.UploadFile.  The problem only appeared when trying to connect to an webserver other than IIS; such as Appache.  The issue revolves around the improper formatting of the ending boundary as defined in the protocol for multipart-form data.  I was able to locate the problem using Microsofts Netmon program and reviewing the raw HTTP packets as they were sent out over the net.  There are basically three boundaries that need to be define: Begining or definition, Content, and Ending.  The Begining boundary basically consists of a bytes array that defines to the receiving end what values to expect as a separator of the multipart data.  The Content boundary consists of the "--" string appended to the begining of the boundary and is used to separate the actual multipart data during the upload.  The Ending boundary consists of the "--" appended to the end of the Content boundary.  As an example the following represents each of these as present in the source code:

BeginBoundary = "ou812--------------8c405ee4e38917c";

ContentBoundary = "--" + BeginBoundary;

EndingBoundary = ContentBoundary + "--";

Another feature not present in the WebClient class was the ability to add form fields into the upload of the data file.  I had a need to allow additional information to be passed along with the uploaded file for verification puposes.  Thus I added the ability in the new class MultipartForm to handle this feature as well.  The class is very straight forward to use:

MultipartForm mp = new MultipartForm(http://localhost/Panasonic/brokerReceiving.aspx);

mp.setField("userid","abcd");

mp.setField("password","mypassword");

mp.sendFile("c:/temp/testfile.txt");

Console.WriteLine( mp.ResponseText);

Note: One other bug in Dot Net HttpWebRequest object, the ProtocolVersion property does nothing.  I needed to send via HTTP 1.0 and setting this value does not follow 1.0 multipart upload protocol.  It still sends via 1.1; I had to request a hot fix from Microsoft to resolve this issue.  The problem I needed to address revolved around an Appache web server that was acting as a proxy to Panasonics back end systems which was IBM webshere.  Appache and or Webshere were both trying to handle the "100 Continue" message in 1.1 multipart upload.  Thus corrupting the protocol; 1.0 should send the upload in one blast vs trying to use the "100 Continue" method.

For more information regarding multipart-form data please review: http://www.ietf.org/rfc/rfc1867.txt

Any question or further explanation may be directed to the authour. gregoryp@norvanco.com