diff --git a/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java b/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java index 664273544..ce2328455 100644 --- a/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java +++ b/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java @@ -29,13 +29,14 @@ package org.apache.http.examples.entity.mime; import java.io.File; import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; -import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; /** @@ -48,13 +49,13 @@ public class ClientMultipartFormPost { System.out.println("File path not given"); System.exit(1); } - HttpClient httpclient = new DefaultHttpClient(); + CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost("http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample"); FileBody bin = new FileBody(new File(args[0])); - StringBody comment = new StringBody("A binary file of some kind"); + StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("bin", bin); @@ -63,17 +64,20 @@ public class ClientMultipartFormPost { httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); - HttpResponse response = httpclient.execute(httppost); - HttpEntity resEntity = response.getEntity(); - - System.out.println("----------------------------------------"); - System.out.println(response.getStatusLine()); - if (resEntity != null) { - System.out.println("Response content length: " + resEntity.getContentLength()); + CloseableHttpResponse response = httpclient.execute(httppost); + try { + System.out.println("----------------------------------------"); + System.out.println(response.getStatusLine()); + HttpEntity resEntity = response.getEntity(); + if (resEntity != null) { + System.out.println("Response content length: " + resEntity.getContentLength()); + } + EntityUtils.consume(resEntity); + } finally { + response.close(); } - EntityUtils.consume(resEntity); } finally { - try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {} + httpclient.close(); } }