You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2010/06/12 12:39:37 UTC
svn commit: r953985 -
/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
Author: olegk
Date: Sat Jun 12 10:39:36 2010
New Revision: 953985
URL: http://svn.apache.org/viewvc?rev=953985&view=rev
Log:
HTTPCLIENT-942: tweaked example
Modified:
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java?rev=953985&r1=953984&r2=953985&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java Sat Jun 12 10:39:36 2010
@@ -27,9 +27,8 @@
package org.apache.http.examples.client;
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -38,7 +37,7 @@ import org.apache.http.client.methods.Ht
import org.apache.http.impl.client.DefaultHttpClient;
/**
- * This example demonstrates the recommended way of using API to make sure
+ * This example demonstrates the recommended way of using API to make sure
* the underlying connection gets released back to the connection manager.
*/
public class ClientConnectionRelease {
@@ -46,7 +45,7 @@ public class ClientConnectionRelease {
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
- HttpGet httpget = new HttpGet("http://www.apache.org/");
+ HttpGet httpget = new HttpGet("http://www.apache.org/");
// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
@@ -58,43 +57,35 @@ public class ClientConnectionRelease {
// Get hold of the response entity
HttpEntity entity = response.getEntity();
-
+
// If the response does not enclose an entity, there is no need
// to bother about connection release
if (entity != null) {
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(entity.getContent()));
+ InputStream instream = entity.getContent();
try {
-
+ instream.read();
// do something useful with the response
- System.out.println(reader.readLine());
-
} catch (IOException ex) {
-
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
-
} catch (RuntimeException ex) {
-
// In case of an unexpected exception you may want to abort
- // the HTTP request in order to shut down the underlying
+ // the HTTP request in order to shut down the underlying
// connection and release it back to the connection manager.
httpget.abort();
throw ex;
-
} finally {
-
// Closing the input stream will trigger connection release
- reader.close();
-
+ instream.close();
+
}
}
- // When HttpClient instance is no longer needed,
+ // When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.getConnectionManager().shutdown();
}
}