Click here to Skip to main content
12,559,389 members (50,325 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked

Send a content type “multipart/form-data” request from C#

, 5 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to send a content type “multipart/form-data” request from C#.


Here we will see a simple procedure to make a request of type "multipart/form-data" from C# using the HttpWebRequest class. We are taking this article as a reference: Send a request to an SSL page from C#.


The big problem of this type of request is how to prepare the data package that will be passed to the class. For this, the more easy way to do is to make a string with the data in text form. You must be careful with this, because if you put the wrong character or separator, the package won't get to the destination successfully.

The next method makes this action about a parameters collection; these parameters can be of different types, because this way you can send the file content.

private string GetPostData() 
    string boundary = "--" + this._Boundary; 
    int arrReqs = this._ParamsCollection.Count * 5; 
    string[] auxReqBody = new string[arrReqs]; 
    int count = 0; 
    foreach(ParamsStruct par in this._ParamsCollection) 
        auxReqBody[count] = boundary; 
        switch (par.Type) 
            case ParamsStruct.ParamType.File: 
                auxReqBody[count] = "Content-Disposition: file; name=\"" + 
                   par.Name + "\"; filename=\"" + par.GetOnlyFileName() + "\""; 
                auxReqBody[count] = "Content-Type: text/plain"; 
                auxReqBody[count] = ""; 
                auxReqBody[count] = par.StringValue; 
            case ParamsStruct.ParamType.Parameter: 
                auxReqBody[count] = 
                  "Content-Disposition: form-data; name=\"" + par.Name + "\""; 
                auxReqBody[count] = ""; 
                auxReqBody[count] = par.StringValue; 
    auxReqBody[count] = boundary; 
    string requestBody = String.Join("\r\n",auxReqBody); 
    return requestBody;

Then in the parameters collection, we have: a parameter of type "Parameter", name "param1", and value "param1Value", and another parameter of type "File", name "file", file name "fileName1.txt", and value "Content of fileName1.txt". After we run this method, you will obtain a text with this format:

Content-Disposition: form-data; name="param1"\r\n 
Content-Disposition: file; name="file"; filename="fileName1.txt"\r\n 
Content-Type: text/plain\r\n 
Content of fileName1.txt \r\n 

After this, we will use a method similar to this one, for sending the information to the required page:

public void PostData ()
    //Set URL 
    Uri urlUri = new Uri(_URL); 
    //Encoding postData 
    ASCIIEncoding encoding = new ASCIIEncoding(); 
    string postData = this.GetPostData(); 
    byte[] buffer = encoding.GetBytes( postData ); 
    // Prepare web request... 
    HttpWebRequest myRequest = 
    // We use POST ( we can also use GET ) 
    myRequest.Method = this._Method; 
    myRequest.AllowWriteStreamBuffering = true; 
    // Set the content type to a FORM 
    string auxContent = this._ContentType; 
    if (this._Boundary.Length > 0) 
        auxContent += "boundary=" + this._Boundary; 
    myRequest.ContentType = auxContent; 
    // Get length of content 
    myRequest.Accept = this._Accept; 
    myRequest.ContentLength = buffer.Length; 
    // Get request stream 
    Stream newStream = myRequest.GetRequestStream(); 
    // Send the data. 
    // Close stream 
    _myHttpWebResponse= (HttpWebResponse)myRequest.GetResponse(); 
    // Set the response to ResponseStream property 
    this._ResponseStream = _myHttpWebResponse.GetResponseStream();

For finishing this, we must process the request stream returned by the required page. In this case, I declare a property inside the class that transforms the response stream to a string.

public string ResponseString
        string resultData = ""; 
        StreamReader streamRead = new StreamReader( _ResponseStream ); 
        Char[] readBuffer = new Char[256]; 
        // Read from buffer 
        int count = streamRead.Read( readBuffer, 0, 256 ); 
        while (count > 0) 
            // get string 
            resultData += new String( readBuffer, 0, count); 
            // Write the data 
            Console.WriteLine( resultData ); 
            // Read from buffer 
            count = streamRead.Read( readBuffer, 0, 256); 
        // Release the response object resources. 
        return resultData; 

In a simple way, this will adapt to another content type. Remember that the key is the form for loading the data package.

For SSL pages, see Send a request to an SSL page from C#.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pablo Russoniello
Web Developer
Argentina Argentina
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDidn't Work :-(. Bad request exception (400) Pin
edazccode26-Nov-09 12:41
memberedazccode26-Nov-09 12:41 
GeneralSending Multiple image Pin
Member 47440291-Apr-09 16:09
memberMember 47440291-Apr-09 16:09 
General{"The operation has timed out"} Pin
Michael Sync27-Aug-08 4:41
memberMichael Sync27-Aug-08 4:41 
Questionbinary data Pin
p. tj16-Apr-07 12:51
memberp. tj16-Apr-07 12:51 
AnswerRe: binary data Pin
stibstibstib7-Jun-07 0:26
memberstibstibstib7-Jun-07 0:26 
GeneralRe: binary data Pin
stibstibstib7-Jun-07 3:01
memberstibstibstib7-Jun-07 3:01 
QuestionRe: binary data(but how to code) Pin
winart22-Jul-07 17:18
memberwinart22-Jul-07 17:18 
GeneralAny difference to post file to JSP page Pin
vdonthi6-Apr-07 8:26
membervdonthi6-Apr-07 8:26 
AnswerRe: Any difference to post file to JSP page Pin
Pablo Russoniello9-Apr-07 4:54
memberPablo Russoniello9-Apr-07 4:54 
Generalopen the POST request in a browser Pin
Zilberbal4-Mar-07 19:40
memberZilberbal4-Mar-07 19:40 
AnswerRe: open the POST request in a browser Pin
Pablo Russoniello5-Mar-07 1:40
memberPablo Russoniello5-Mar-07 1:40 
GeneralRe: open the POST request in a browser Pin
Zilberbal8-Mar-07 0:17
memberZilberbal8-Mar-07 0:17 
Questionhow to read xml data from a http post Pin
deepa l22-Feb-07 12:16
memberdeepa l22-Feb-07 12:16 
AnswerRe: how to read xml data from a http post Pin
Pablo Russoniello23-Feb-07 1:53
memberPablo Russoniello23-Feb-07 1:53 
Generalread xml data from a http post Pin
Member #384406922-Feb-07 12:13
memberMember #384406922-Feb-07 12:13 
QuestionTried to use your class Pin
alpha_krone21-Feb-07 21:13
memberalpha_krone21-Feb-07 21:13 
AnswerRe: Tried to use your class Pin
Pablo Russoniello22-Feb-07 1:31
memberPablo Russoniello22-Feb-07 1:31 
GeneralRe: Tried to use your class Pin
alpha_krone22-Feb-07 15:04
memberalpha_krone22-Feb-07 15:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161026.1 | Last Updated 5 Mar 2007
Article Copyright 2007 by Pablo Russoniello
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid