hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1426534 - /httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
Date Fri, 28 Dec 2012 14:59:30 GMT
Author: olegk
Date: Fri Dec 28 14:59:30 2012
New Revision: 1426534

URL: http://svn.apache.org/viewvc?rev=1426534&view=rev
Log:
Updated multipart POST example

Modified:
    httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

Modified: httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java?rev=1426534&r1=1426533&r2=1426534&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
(original)
+++ httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
Fri Dec 28 14:59:30 2012
@@ -29,13 +29,14 @@ package org.apache.http.examples.entity.
 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();
         }
     }
 



Mime
View raw message