Multipage POST with file and string in HTTPClient 4.1

I need to create a multi-threaded POST request containing the fields: update[image_title] = String update[image] = image-data itself . As you can see, both of them are in an associative array called "update". How could I do this with HTTPClient 4.1 because I only found examples for line 3.x of this library.

Thanks in advance.

+6
source share
2 answers

Maybe too late, but it might help someone. I had the same problem. Assuming you have a file object that has the necessary image information

 HttpPost post = new HttpPost(YOUR_URL); MultipartEntity entity = new MultipartEntity(); ByteArrayBody body = new ByteArrayBody(file.getData(), file.getName()); String imageTitle = new StringBody(file.getName()); entity.addPart("imageTitle", imageTitle); entity.addPart("image", body); post.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = null; try { response = client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

Please note that MultiPartEntity is part of the HttpMime module. Therefore, you need to put this jar in the lib directory or include the dependency (maven / gradle) in it.

+13
source

Yes, I found a real pain to find examples of HTTP Client 4, etc., since the omnipotent Google almost always still points to HTTP 3.

Anyway, the last sample on this page - http://hc.apache.org/httpcomponents-client-ga/examples.html should be what you want.

+1
source

All Articles