This code can be used in order to send a Multipart Form as part of a POST request with parameters. This kind of POST request will usually be used to send Files, but there some cases where you will need this without sending any files. Next paragraphs will explain the code.

First we create the HttpClient object and the POST method object. (lines 1-2)
Then we create a MutipartEntity. This type of entity enables us to send both string parameters and files in the same request. We then assign the MultipartEntity to be the PostMethod’s entity. (line 4-5)

Now we create a StringBody. We use StringBody to pass regular string parameters (very much the same as a regular POST request, only this is the way it done for a Multipart request). We add the StringBody to the MultipartEntity.(lines 7-8)
We perform this step twice, once for each of the parameters (firstName and lastName).

The rest of the code is pretty straight forward. We execute the request, and print the response status code, and content.


HttpClient client = new HttpClient();
HttpPost postMethod = new HttpPost(new Uri("http://www.w3schools.com/asp/demo_simpleform.asp"));

MultipartEntity multipartEntity = new MultipartEntity();
postMethod.Entity = multipartEntity;

StringBody stringBody = new StringBody(Encoding.UTF8, "firstName", "brian");
multipartEntity.AddBody(stringBody);

StringBody stringBody2 = new StringBody(Encoding.UTF8, "lastName", "star");
multipartEntity.AddBody(stringBody2);
            
HttpResponse response = client.Execute(postMethod);

Console.WriteLine("Response Code: " + response.ResponseCode);
Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));

For this to work you will need the following using directives:

using CodeScales.Http;
using CodeScales.Http.Methods;
using CodeScales.Http.Entity;
using CodeScales.Http.Entity.Mime;
using CodeScales.Http.Common;