views:

327

answers:

2

I'm pretty green to HttpClient and I'm finding the lack of (and or blatantly incorrect) documentation extremely frustrating. I'm trying to implement the following post (listed below) with Apache Http Client, but have no idea how to actually do it. I'm going to bury myself in documentation for the next week, but perhaps more experienced HttpClient coders could get me an answer sooner.

Post:

Content-Type: multipart/form-data; boundary=---------------------------1294919323195
Content-Length: 502
-----------------------------1294919323195
Content-Disposition: form-data; name="number"

5555555555
-----------------------------1294919323195
Content-Disposition: form-data; name="clip"

rickroll
-----------------------------1294919323195
Content-Disposition: form-data; name="upload_file"; filename=""
Content-Type: application/octet-stream


-----------------------------1294919323195
Content-Disposition: form-data; name="tos"

agree
-----------------------------1294919323195--
+1  A: 

Hi.

Use MultipartEntity to perform request you want. In my project I do that this way:

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("number", new StringBody("5555555555"));
entity.addPart("clip", new StringBody("rickroll"));
File fileToUpload = new File(filePath);
FileBody fileBody = new FileBody(fileToUpload, "application/octet-stream");
entity.addPart("upload_file", fileBody);
entity.addPart("tos", new StringBody("agree"));

HttpPost httpPost = new HttpPost("http://some-web-site");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();

Hope this will help.

mtomy
A: 

Here is a snippet that shows how to use post method with params in httpclient 4.

php html
The linked example uses a `UrlEncodedFormEntity` (`application/x-www-form-urlencoded`), while @Russ would like to do a multipart/form-data POST. Instead, you should use a `MultipartEntity`.
Alessandro Vernet