From e8bb17f4e23c6f6d5ae146ecf45f83dc0686d2ca Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Thu, 21 Oct 2010 19:57:32 +0000 Subject: [PATCH] Fixed examples git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1026120 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/examples/client/ClientAuthentication.java | 5 ++--- .../http/examples/client/ClientChunkEncodedPost.java | 5 ++--- .../http/examples/client/ClientCustomContext.java | 5 ++--- .../apache/http/examples/client/ClientCustomSSL.java | 5 ++--- .../examples/client/ClientEvictExpiredConnections.java | 7 +++---- .../apache/http/examples/client/ClientFormLogin.java | 10 ++++------ .../client/ClientInteractiveAuthentication.java | 5 ++--- .../examples/client/ClientKerberosAuthentication.java | 4 +--- .../examples/client/ClientMultiThreadedExecution.java | 2 +- .../client/ClientPreemptiveBasicAuthentication.java | 3 ++- .../client/ClientPreemptiveDigestAuthentication.java | 3 ++- .../examples/client/ClientProxyAuthentication.java | 5 ++--- 12 files changed, 25 insertions(+), 34 deletions(-) diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java index 45991bc5e..aefb5d0d9 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java @@ -31,6 +31,7 @@ import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; /** * A simple example that uses HttpClient to execute an HTTP request against @@ -56,9 +57,7 @@ public class ClientAuthentication { if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java b/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java index 8d057f8e1..fb96da959 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java @@ -35,6 +35,7 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.InputStreamEntity; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; /** * Example how to use unbuffered chunk-encoded POST request. @@ -75,9 +76,7 @@ public class ClientChunkEncodedPost { System.out.println("Response content length: " + resEntity.getContentLength()); System.out.println("Chunked?: " + resEntity.isChunked()); } - if (resEntity != null) { - resEntity.consumeContent(); - } + EntityUtils.consume(resEntity); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java b/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java index ff22d5573..215cbb96a 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java @@ -40,6 +40,7 @@ import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.util.EntityUtils; /** @@ -79,9 +80,7 @@ public class ClientCustomContext { } // Consume response content - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); System.out.println("----------------------------------------"); diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java b/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java index 857393e56..82efacbf0 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java @@ -36,6 +36,7 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; /** * This example demonstrates how to create secure connections with a custom SSL @@ -70,9 +71,7 @@ public class ClientCustomSSL { if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java b/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java index e60289668..a96ee5fbc 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java @@ -38,6 +38,7 @@ import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; +import org.apache.http.util.EntityUtils; /** * Example demonstrating how to evict expired and idle connections @@ -52,7 +53,7 @@ public class ClientEvictExpiredConnections { new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); - cm.setMaxTotalConnections(100); + cm.setMaxTotal(100); HttpClient httpclient = new DefaultHttpClient(cm); @@ -83,9 +84,7 @@ public class ClientEvictExpiredConnections { } System.out.println("----------------------------------------"); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); } // Sleep 10 sec and let the connection evictor do its job diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java b/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java index 6c0b33e7c..f0ec7c726 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java @@ -38,6 +38,7 @@ import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; /** * A example that demonstrates how HttpClient APIs can be used to perform @@ -55,9 +56,8 @@ public class ClientFormLogin { HttpEntity entity = response.getEntity(); System.out.println("Login form get: " + response.getStatusLine()); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); + System.out.println("Initial set of cookies:"); List cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { @@ -83,9 +83,7 @@ public class ClientFormLogin { entity = response.getEntity(); System.out.println("Login form get: " + response.getStatusLine()); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); System.out.println("Post logon cookies:"); cookies = httpclient.getCookieStore().getCookies(); diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java index d6ca8390c..4f01a6fbf 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java @@ -40,6 +40,7 @@ import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; +import org.apache.http.util.EntityUtils; /** * A simple example that uses HttpClient to execute an HTTP request against @@ -65,9 +66,7 @@ public class ClientInteractiveAuthentication { // Consume response content HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); int sc = response.getStatusLine().getStatusCode(); diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java index 25ab285c4..bb76719e8 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java @@ -162,9 +162,7 @@ public class ClientKerberosAuthentication { System.out.println("----------------------------------------"); // This ensures the connection gets released back to the manager - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java b/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java index c96e5ecf8..7a56a2225 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java @@ -55,7 +55,7 @@ public class ClientMultiThreadedExecution { // This connection manager must be used if more than one thread will // be using the HttpClient. ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); - cm.setMaxTotalConnections(100); + cm.setMaxTotal(100); HttpClient httpClient = new DefaultHttpClient(cm); diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java index 36457172b..3f76a3b2c 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java @@ -37,6 +37,7 @@ import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.util.EntityUtils; /** * An example of HttpClient can be customized to authenticate @@ -82,8 +83,8 @@ public class ClientPreemptiveBasicAuthentication { System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); - entity.consumeContent(); } + EntityUtils.consume(entity); } // When HttpClient instance is no longer needed, diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java index ebf747539..99645142d 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java @@ -37,6 +37,7 @@ import org.apache.http.impl.auth.DigestScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.util.EntityUtils; /** * An example of HttpClient can be customized to authenticate @@ -86,8 +87,8 @@ public class ClientPreemptiveDigestAuthentication { System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); - entity.consumeContent(); } + EntityUtils.consume(entity); } // When HttpClient instance is no longer needed, diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java b/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java index 8b3af35b7..035f7093a 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java @@ -33,6 +33,7 @@ import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; /** * A simple example that uses HttpClient to execute an HTTP request @@ -67,9 +68,7 @@ public class ClientProxyAuthentication { if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure