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 2012/11/04 08:40:03 UTC
svn commit: r1405509 [1/3] - in /httpcomponents/httpclient/trunk:
fluent-hc/src/main/java/org/apache/http/client/fluent/
httpclient/src/examples/org/apache/http/examples/client/
httpclient/src/main/java/org/apache/http/client/ httpclient/src/main/java/...
Author: olegk
Date: Sun Nov 4 07:40:00 2012
New Revision: 1405509
URL: http://svn.apache.org/viewvc?rev=1405509&view=rev
Log:
HttpClient resource management API made compatible with Java 7 'try with resources': HttpClientBuilder to return CloseableHttpClient and CloseableHttpClient to return CloseableHttpResponse objects which implement java.io.Closeable; made ClientChainExec interface and related classes package private
Added:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/CloseableHttpResponse.java
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/CloseableHttpClient.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractBasicHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/CloseableHttpResponseProxy.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClients.java
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/BackoffStrategyExec.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/BackoffStrategyExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/ClientExecChain.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ClientExecChain.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/ConnectionReleaseTriggerImpl.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ConnectionReleaseTriggerImpl.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/HttpClientBuilder.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/HttpRequestWrapper.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/HttpRequestWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/HttpResponseWrapper.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/HttpResponseWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/InternalHttpClient.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/MainClientExec.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/ProtocolExec.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/RedirectExec.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/RedirectExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/RetryExec.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/builder/ServiceUnavailableRetryExec.java (contents, props changed)
- copied, changed from r1405508, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ServiceUnavailableRetryExec.java
Removed:
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientKerberosAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractBasicHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/BackoffStrategyExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ClientExecChain.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ConnectionReleaseTriggerImpl.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/HttpRequestWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/HttpResponseWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ProtocolExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/RedirectExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/RetryExec.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/exec/ServiceUnavailableRetryExec.java
Modified:
httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAbortMethod.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/QuickStart.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/HttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/HttpClientUtils.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AutoRetryHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DecompressingHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/EntityEnclosingRequestWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/RequestWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/SystemDefaultHttpClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/TestConnectionReuse.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRequestRetryHandler.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/IntegrationTestBase.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientRequestExecution.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestContentCodings.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestCookie2Support.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestIdleConnectionEviction.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestRedirects.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestSPNegoScheme.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestStatefulConnManagement.java
Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java Sun Nov 4 07:40:00 2012
@@ -54,7 +54,7 @@ import org.apache.http.conn.ssl.SSLSocke
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
-import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.builder.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;
@@ -93,7 +93,7 @@ public class Executor {
CONNMGR = new PoolingHttpClientConnectionManager(schemeRegistry);
CONNMGR.setDefaultMaxPerRoute(100);
CONNMGR.setMaxTotal(200);
- CLIENT = new HttpClientBuilder().setConnectionManager(CONNMGR).build();
+ CLIENT = HttpClientBuilder.create().setConnectionManager(CONNMGR).build();
}
public static Executor newInstance() {
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAbortMethod.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAbortMethod.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAbortMethod.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAbortMethod.java Sun Nov 4 07:40:00 2012
@@ -28,10 +28,10 @@
package org.apache.http.examples.client;
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.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.client.CloseableHttpClient;
/**
* This example demonstrates how to abort an HTTP method before its normal completion.
@@ -39,29 +39,30 @@ import org.apache.http.impl.client.Defau
public class ClientAbortMethod {
public final static void main(String[] args) throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
+ CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
System.out.println("executing request " + httpget.getURI());
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ CloseableHttpResponse response = httpclient.execute(httpget);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ System.out.println("----------------------------------------");
+
+ // Do not feel like reading the response body
+ // Call abort on the request object
+ httpget.abort();
+ } finally {
+ response.close();
}
- System.out.println("----------------------------------------");
-
- // Do not feel like reading the response body
- // Call abort on the request object
- httpget.abort();
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java Sun Nov 4 07:40:00 2012
@@ -26,11 +26,14 @@
package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
@@ -40,29 +43,31 @@ import org.apache.http.util.EntityUtils;
public class ClientAuthentication {
public static void main(String[] args) throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope("localhost", 443),
+ new UsernamePasswordCredentials("username", "password"));
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setCredentialsProvider(credsProvider).build();
try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope("localhost", 443),
- new UsernamePasswordCredentials("username", "password"));
-
HttpGet httpget = new HttpGet("https://localhost/protected");
System.out.println("executing request" + httpget.getRequestLine());
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ CloseableHttpResponse response = httpclient.execute(httpget);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
}
- EntityUtils.consume(entity);
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java Sun Nov 4 07:40:00 2012
@@ -30,11 +30,11 @@ import java.io.File;
import java.io.FileInputStream;
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.InputStreamEntity;
-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;
/**
@@ -47,7 +47,7 @@ public class ClientChunkEncodedPost {
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");
@@ -67,21 +67,22 @@ public class ClientChunkEncodedPost {
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());
- System.out.println("Chunked?: " + resEntity.isChunked());
+ CloseableHttpResponse response = httpclient.execute(httppost);
+ try {
+ HttpEntity resEntity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (resEntity != null) {
+ System.out.println("Response content length: " + resEntity.getContentLength());
+ System.out.println("Chunked?: " + resEntity.isChunked());
+ }
+ EntityUtils.consume(resEntity);
+ } finally {
+ response.close();
}
- EntityUtils.consume(resEntity);
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
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=1405509&r1=1405508&r2=1405509&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 Sun Nov 4 07:40:00 2012
@@ -31,10 +31,10 @@ import java.io.IOException;
import java.io.InputStream;
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.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
/**
* This example demonstrates the recommended way of using API to make sure
@@ -43,49 +43,42 @@ import org.apache.http.impl.client.Defau
public class ClientConnectionRelease {
public final static void main(String[] args) throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
+ CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
- HttpResponse response = httpclient.execute(httpget);
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- System.out.println("----------------------------------------");
-
- // 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) {
- InputStream instream = entity.getContent();
- try {
- instream.read();
- // do something useful with the response
- } 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
- // connection immediately.
- httpget.abort();
- throw ex;
- } finally {
- // Closing the input stream will trigger connection release
- try { instream.close(); } catch (Exception ignore) {}
+ CloseableHttpResponse response = httpclient.execute(httpget);
+ try {
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ System.out.println("----------------------------------------");
+
+ // 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) {
+ InputStream instream = entity.getContent();
+ try {
+ instream.read();
+ // do something useful with the response
+ } catch (IOException ex) {
+ // In case of an IOException the connection will be released
+ // back to the connection manager automatically
+ throw ex;
+ } finally {
+ // Closing the input stream will trigger connection release
+ instream.close();
+ }
}
+ } finally {
+ response.close();
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java Sun Nov 4 07:40:00 2012
@@ -30,19 +30,18 @@ package org.apache.http.examples.client;
import java.util.List;
import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
-import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.HttpContext;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
-
/**
* This example demonstrates the use of a local HTTP context populated with
* custom attributes.
@@ -50,8 +49,7 @@ import org.apache.http.util.EntityUtils;
public class ClientCustomContext {
public final static void main(String[] args) throws Exception {
-
- HttpClient httpclient = new DefaultHttpClient();
+ CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
@@ -66,29 +64,29 @@ public class ClientCustomContext {
System.out.println("executing request " + httpget.getURI());
// Pass local context as a parameter
- HttpResponse response = httpclient.execute(httpget, localContext);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ CloseableHttpResponse response = httpclient.execute(httpget, localContext);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ List<Cookie> cookies = cookieStore.getCookies();
+ for (int i = 0; i < cookies.size(); i++) {
+ System.out.println("Local cookie: " + cookies.get(i));
+ }
+
+ // Consume response content
+ EntityUtils.consume(entity);
+
+ System.out.println("----------------------------------------");
+ } finally {
+ response.close();
}
- List<Cookie> cookies = cookieStore.getCookies();
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("Local cookie: " + cookies.get(i));
- }
-
- // Consume response content
- EntityUtils.consume(entity);
-
- System.out.println("----------------------------------------");
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java Sun Nov 4 07:40:00 2012
@@ -31,11 +31,11 @@ import java.io.FileInputStream;
import java.security.KeyStore;
import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
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.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
@@ -45,39 +45,36 @@ import org.apache.http.util.EntityUtils;
public class ClientCustomSSL {
public final static void main(String[] args) throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ FileInputStream instream = new FileInputStream(new File("my.keystore"));
+ try {
+ trustStore.load(instream, "nopassword".toCharArray());
+ } finally {
+ instream.close();
+ }
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setSSLSocketFactory(new SSLSocketFactory(trustStore)).build();
try {
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream instream = new FileInputStream(new File("my.keystore"));
- try {
- trustStore.load(instream, "nopassword".toCharArray());
- } finally {
- try { instream.close(); } catch (Exception ignore) {}
- }
-
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- Scheme sch = new Scheme("https", 443, socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://localhost/");
System.out.println("executing request" + httpget.getRequestLine());
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
+ CloseableHttpResponse response = httpclient.execute(httpget);
+ try {
+ HttpEntity entity = response.getEntity();
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
}
- EntityUtils.consume(entity);
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java Sun Nov 4 07:40:00 2012
@@ -29,12 +29,12 @@ package org.apache.http.examples.client;
import java.util.concurrent.TimeUnit;
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.HttpGet;
-import org.apache.http.conn.ClientConnectionManager;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
/**
@@ -44,10 +44,10 @@ import org.apache.http.util.EntityUtils;
public class ClientEvictExpiredConnections {
public static void main(String[] args) throws Exception {
- PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
-
- HttpClient httpclient = new DefaultHttpClient(cm);
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setConnectionManager(cm).build();
try {
// create an array of URIs to perform GETs on
String[] urisToGet = {
@@ -62,21 +62,25 @@ public class ClientEvictExpiredConnectio
for (int i = 0; i < urisToGet.length; i++) {
String requestURI = urisToGet[i];
- HttpGet req = new HttpGet(requestURI);
+ HttpGet request = new HttpGet(requestURI);
System.out.println("executing request " + requestURI);
- HttpResponse rsp = httpclient.execute(req);
- HttpEntity entity = rsp.getEntity();
+ CloseableHttpResponse response = httpclient.execute(request);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ System.out.println("----------------------------------------");
- System.out.println("----------------------------------------");
- System.out.println(rsp.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
}
- System.out.println("----------------------------------------");
-
- EntityUtils.consume(entity);
}
// Sleep 10 sec and let the connection evictor do its job
@@ -87,20 +91,17 @@ public class ClientEvictExpiredConnectio
connEvictor.join();
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
public static class IdleConnectionEvictor extends Thread {
- private final ClientConnectionManager connMgr;
+ private final HttpClientConnectionManager connMgr;
private volatile boolean shutdown;
- public IdleConnectionEvictor(ClientConnectionManager connMgr) {
+ public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
super();
this.connMgr = connMgr;
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java Sun Nov 4 07:40:00 2012
@@ -30,10 +30,11 @@ package org.apache.http.examples.client;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
-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;
/**
@@ -44,33 +45,33 @@ import org.apache.http.util.EntityUtils;
public class ClientExecuteDirect {
public static void main(String[] args) throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpHost target = new HttpHost("www.apache.org", 80, "http");
- HttpGet req = new HttpGet("/");
+ HttpGet request = new HttpGet("/");
System.out.println("executing request to " + target);
- HttpResponse rsp = httpclient.execute(target, req);
- HttpEntity entity = rsp.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(rsp.getStatusLine());
- Header[] headers = rsp.getAllHeaders();
- for (int i = 0; i < headers.length; i++) {
- System.out.println(headers[i]);
- }
- System.out.println("----------------------------------------");
-
- if (entity != null) {
- System.out.println(EntityUtils.toString(entity));
+ CloseableHttpResponse response = httpclient.execute(target, request);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ Header[] headers = response.getAllHeaders();
+ for (int i = 0; i < headers.length; i++) {
+ System.out.println(headers[i]);
+ }
+ System.out.println("----------------------------------------");
+
+ if (entity != null) {
+ System.out.println(EntityUtils.toString(entity));
+ }
+ } finally {
+ response.close();
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java Sun Nov 4 07:40:00 2012
@@ -30,11 +30,12 @@ package org.apache.http.examples.client;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
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.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
@@ -46,35 +47,34 @@ public class ClientExecuteProxy {
public static void main(String[] args)throws Exception {
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
-
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ CloseableHttpClient httpclient = HttpClients.createDefault();
try {
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpHost target = new HttpHost("issues.apache.org", 443, "https");
- HttpGet req = new HttpGet("/");
+ HttpGet request = new HttpGet("/");
System.out.println("executing request to " + target + " via " + proxy);
- HttpResponse rsp = httpclient.execute(target, req);
- HttpEntity entity = rsp.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(rsp.getStatusLine());
- Header[] headers = rsp.getAllHeaders();
- for (int i = 0; i<headers.length; i++) {
- System.out.println(headers[i]);
+ CloseableHttpResponse response = httpclient.execute(target, request);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ Header[] headers = response.getAllHeaders();
+ for (int i = 0; i<headers.length; i++) {
+ System.out.println(headers[i]);
+ }
+ System.out.println("----------------------------------------");
+
+ if (entity != null) {
+ System.out.println(EntityUtils.toString(entity));
+ }
+ } finally {
+ response.close();
}
- System.out.println("----------------------------------------");
-
- if (entity != null) {
- System.out.println(EntityUtils.toString(entity));
- }
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java Sun Nov 4 07:40:00 2012
@@ -37,13 +37,17 @@ import java.net.UnknownHostException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SchemeSocketFactory;
-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.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
@@ -56,37 +60,38 @@ import org.apache.http.util.EntityUtils;
public class ClientExecuteSOCKS {
public static void main(String[] args)throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
+ schemeRegistry.register(new Scheme("http", 80, new MySchemeSocketFactory()));
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
+ CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
try {
httpclient.getParams().setParameter("socks.host", "mysockshost");
httpclient.getParams().setParameter("socks.port", 1234);
- httpclient.getConnectionManager().getSchemeRegistry().register(
- new Scheme("http", 80, new MySchemeSocketFactory()));
HttpHost target = new HttpHost("www.apache.org", 80, "http");
- HttpGet req = new HttpGet("/");
+ HttpGet request = new HttpGet("/");
System.out.println("executing request to " + target + " via SOCKS proxy");
- HttpResponse rsp = httpclient.execute(target, req);
- HttpEntity entity = rsp.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(rsp.getStatusLine());
- Header[] headers = rsp.getAllHeaders();
- for (int i = 0; i<headers.length; i++) {
- System.out.println(headers[i]);
- }
- System.out.println("----------------------------------------");
+ CloseableHttpResponse response = httpclient.execute(target, request);
+ try {
+ HttpEntity entity = response.getEntity();
- if (entity != null) {
- System.out.println(EntityUtils.toString(entity));
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ Header[] headers = response.getAllHeaders();
+ for (int i = 0; i<headers.length; i++) {
+ System.out.println(headers[i]);
+ }
+ System.out.println("----------------------------------------");
+
+ if (entity != null) {
+ System.out.println(EntityUtils.toString(entity));
+ }
+ } finally {
+ response.close();
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java Sun Nov 4 07:40:00 2012
@@ -31,13 +31,15 @@ import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.BasicCookieStore;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
@@ -48,59 +50,62 @@ import org.apache.http.util.EntityUtils;
public class ClientFormLogin {
public static void main(String[] args) throws Exception {
-
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ BasicCookieStore cookieStore = new BasicCookieStore();
+ CloseableHttpClient httpclient = HttpClients.custom().setCookieStore(cookieStore).build();
try {
HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println("Login form get: " + response.getStatusLine());
- EntityUtils.consume(entity);
-
- System.out.println("Initial set of cookies:");
- List<Cookie> cookies = httpclient.getCookieStore().getCookies();
- if (cookies.isEmpty()) {
- System.out.println("None");
- } else {
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("- " + cookies.get(i).toString());
+ CloseableHttpResponse response1 = httpclient.execute(httpget);
+ try {
+ HttpEntity entity = response1.getEntity();
+
+ System.out.println("Login form get: " + response1.getStatusLine());
+ EntityUtils.consume(entity);
+
+ System.out.println("Initial set of cookies:");
+ List<Cookie> cookies = cookieStore.getCookies();
+ if (cookies.isEmpty()) {
+ System.out.println("None");
+ } else {
+ for (int i = 0; i < cookies.size(); i++) {
+ System.out.println("- " + cookies.get(i).toString());
+ }
}
+ } finally {
+ response1.close();
}
HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
"org=self_registered_users&" +
"goto=/portal/dt&" +
"gotoOnFail=/portal/dt?error=true");
-
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
- response = httpclient.execute(httpost);
- entity = response.getEntity();
-
- System.out.println("Login form get: " + response.getStatusLine());
- EntityUtils.consume(entity);
-
- System.out.println("Post logon cookies:");
- cookies = httpclient.getCookieStore().getCookies();
- if (cookies.isEmpty()) {
- System.out.println("None");
- } else {
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("- " + cookies.get(i).toString());
+ CloseableHttpResponse response2 = httpclient.execute(httpost);
+ try {
+ HttpEntity entity = response2.getEntity();
+
+ System.out.println("Login form get: " + response2.getStatusLine());
+ EntityUtils.consume(entity);
+
+ System.out.println("Post logon cookies:");
+ List<Cookie> cookies = cookieStore.getCookies();
+ if (cookies.isEmpty()) {
+ System.out.println("None");
+ } else {
+ for (int i = 0; i < cookies.size(); i++) {
+ System.out.println("- " + cookies.get(i).toString());
+ }
}
+ } finally {
+ response2.close();
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientGZipContentCompression.java Sun Nov 4 07:40:00 2012
@@ -38,8 +38,10 @@ import org.apache.http.HttpRequestInterc
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.entity.GzipDecompressingEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
-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.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
@@ -59,9 +61,8 @@ import org.apache.http.util.EntityUtils;
public class ClientGZipContentCompression {
public final static void main(String[] args) throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .addInterceptorFirst(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
@@ -69,11 +70,8 @@ public class ClientGZipContentCompressio
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
- }
-
- });
- httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
+ }}).addInterceptorFirst(new HttpResponseInterceptor() {
public void process(
final HttpResponse response,
@@ -94,34 +92,33 @@ public class ClientGZipContentCompressio
}
}
- });
-
+ }).build();
+ try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
- HttpResponse response = httpclient.execute(httpget);
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- System.out.println(response.getLastHeader("Content-Encoding"));
- System.out.println(response.getLastHeader("Content-Length"));
- System.out.println("----------------------------------------");
-
- HttpEntity entity = response.getEntity();
-
- if (entity != null) {
- String content = EntityUtils.toString(entity);
- System.out.println(content);
+ CloseableHttpResponse response = httpclient.execute(httpget);
+ try {
System.out.println("----------------------------------------");
- System.out.println("Uncompressed size: "+content.length());
- }
+ System.out.println(response.getStatusLine());
+ System.out.println(response.getLastHeader("Content-Encoding"));
+ System.out.println(response.getLastHeader("Content-Length"));
+ System.out.println("----------------------------------------");
+
+ HttpEntity entity = response.getEntity();
+ if (entity != null) {
+ String content = EntityUtils.toString(entity);
+ System.out.println(content);
+ System.out.println("----------------------------------------");
+ System.out.println("Uncompressed size: "+content.length());
+ }
+ } finally {
+ response.close();
+ }
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientInteractiveAuthentication.java Sun Nov 4 07:40:00 2012
@@ -30,16 +30,18 @@ import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
@@ -52,7 +54,9 @@ import org.apache.http.util.EntityUtils;
public class ClientInteractiveAuthentication {
public static void main(String[] args) throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setCredentialsProvider(credsProvider).build();
try {
// Create local execution context
HttpContext localContext = new BasicHttpContext();
@@ -62,60 +66,59 @@ public class ClientInteractiveAuthentica
boolean trying = true;
while (trying) {
System.out.println("executing request " + httpget.getRequestLine());
- HttpResponse response = httpclient.execute(httpget, localContext);
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
+ CloseableHttpResponse response = httpclient.execute(httpget, localContext);
+ try {
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
- // Consume response content
- HttpEntity entity = response.getEntity();
- EntityUtils.consume(entity);
-
- int sc = response.getStatusLine().getStatusCode();
-
- AuthState authState = null;
- HttpHost authhost = null;
- if (sc == HttpStatus.SC_UNAUTHORIZED) {
- // Target host authentication required
- authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
- authhost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
- }
- if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
- // Proxy authentication required
- authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
- authhost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
- }
+ // Consume response content
+ HttpEntity entity = response.getEntity();
+ EntityUtils.consume(entity);
+
+ int sc = response.getStatusLine().getStatusCode();
+
+ AuthState authState = null;
+ HttpHost authhost = null;
+ if (sc == HttpStatus.SC_UNAUTHORIZED) {
+ // Target host authentication required
+ authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
+ authhost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
+ }
+ if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
+ // Proxy authentication required
+ authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
+ authhost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
+ }
- if (authState != null) {
- System.out.println("----------------------------------------");
- AuthScheme authscheme = authState.getAuthScheme();
- System.out.println("Please provide credentials for " +
- authscheme.getRealm() + "@" + authhost.toHostString());
-
- BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
-
- System.out.print("Enter username: ");
- String user = console.readLine();
- System.out.print("Enter password: ");
- String password = console.readLine();
-
- if (user != null && user.length() > 0) {
- Credentials creds = new UsernamePasswordCredentials(user, password);
- httpclient.getCredentialsProvider().setCredentials(new AuthScope(authhost), creds);
- trying = true;
+ if (authState != null) {
+ System.out.println("----------------------------------------");
+ AuthScheme authscheme = authState.getAuthScheme();
+ System.out.println("Please provide credentials for " +
+ authscheme.getRealm() + "@" + authhost.toHostString());
+
+ BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
+
+ System.out.print("Enter username: ");
+ String user = console.readLine();
+ System.out.print("Enter password: ");
+ String password = console.readLine();
+
+ if (user != null && user.length() > 0) {
+ Credentials creds = new UsernamePasswordCredentials(user, password);
+ credsProvider.setCredentials(new AuthScope(authhost), creds);
+ trying = true;
+ } else {
+ trying = false;
+ }
} else {
trying = false;
}
- } else {
- trying = false;
+ } finally {
+ response.close();
}
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java Sun Nov 4 07:40:00 2012
@@ -27,11 +27,11 @@
package org.apache.http.examples.client;
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.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
@@ -46,10 +46,10 @@ public class ClientMultiThreadedExecutio
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
- PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
- HttpClient httpclient = new DefaultHttpClient(cm);
+ CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
try {
// create an array of URIs to perform GETs on
String[] urisToGet = {
@@ -77,10 +77,7 @@ public class ClientMultiThreadedExecutio
}
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
@@ -89,12 +86,12 @@ public class ClientMultiThreadedExecutio
*/
static class GetThread extends Thread {
- private final HttpClient httpClient;
+ private final CloseableHttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
private final int id;
- public GetThread(HttpClient httpClient, HttpGet httpget, int id) {
+ public GetThread(CloseableHttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpget = httpget;
@@ -106,24 +103,21 @@ public class ClientMultiThreadedExecutio
*/
@Override
public void run() {
-
- System.out.println(id + " - about to get something from " + httpget.getURI());
-
try {
-
- // execute the method
- HttpResponse response = httpClient.execute(httpget, context);
-
- System.out.println(id + " - get executed");
- // get the response body as an array of bytes
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- byte[] bytes = EntityUtils.toByteArray(entity);
- System.out.println(id + " - " + bytes.length + " bytes read");
+ System.out.println(id + " - about to get something from " + httpget.getURI());
+ CloseableHttpResponse response = httpClient.execute(httpget, context);
+ try {
+ System.out.println(id + " - get executed");
+ // get the response body as an array of bytes
+ HttpEntity entity = response.getEntity();
+ if (entity != null) {
+ byte[] bytes = EntityUtils.toByteArray(entity);
+ System.out.println(id + " - " + bytes.length + " bytes read");
+ }
+ } finally {
+ response.close();
}
-
} catch (Exception e) {
- httpget.abort();
System.out.println(id + " - error: " + e);
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java Sun Nov 4 07:40:00 2012
@@ -27,15 +27,18 @@ package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
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.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
@@ -50,14 +53,14 @@ import org.apache.http.util.EntityUtils;
public class ClientPreemptiveBasicAuthentication {
public static void main(String[] args) throws Exception {
-
HttpHost targetHost = new HttpHost("localhost", 80, "http");
-
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope(targetHost.getHostName(), targetHost.getPort()),
+ new UsernamePasswordCredentials("username", "password"));
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setCredentialsProvider(credsProvider).build();
try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope(targetHost.getHostName(), targetHost.getPort()),
- new UsernamePasswordCredentials("username", "password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
@@ -76,22 +79,22 @@ public class ClientPreemptiveBasicAuthen
System.out.println("to target: " + targetHost);
for (int i = 0; i < 3; i++) {
- HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ CloseableHttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
}
- EntityUtils.consume(entity);
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java Sun Nov 4 07:40:00 2012
@@ -27,22 +27,25 @@ package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
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.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
/**
* An example of HttpClient can be customized to authenticate
* preemptively using DIGEST scheme.
- * <b/>
+ * <p/>
* Generally, preemptive authentication can be considered less
* secure than a response to an authentication challenge
* and therefore discouraged.
@@ -50,14 +53,14 @@ import org.apache.http.util.EntityUtils;
public class ClientPreemptiveDigestAuthentication {
public static void main(String[] args) throws Exception {
-
HttpHost targetHost = new HttpHost("localhost", 80, "http");
-
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope(targetHost.getHostName(), targetHost.getPort()),
+ new UsernamePasswordCredentials("username", "password"));
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setCredentialsProvider(credsProvider).build();
try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope(targetHost.getHostName(), targetHost.getPort()),
- new UsernamePasswordCredentials("username", "password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
@@ -80,22 +83,22 @@ public class ClientPreemptiveDigestAuthe
System.out.println("to target: " + targetHost);
for (int i = 0; i < 3; i++) {
- HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ CloseableHttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
}
- EntityUtils.consume(entity);
}
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java?rev=1405509&r1=1405508&r2=1405509&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java Sun Nov 4 07:40:00 2012
@@ -27,12 +27,15 @@ package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.CloseableHttpResponse;
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.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
@@ -42,13 +45,13 @@ import org.apache.http.util.EntityUtils;
public class ClientProxyAuthentication {
public static void main(String[] args) throws Exception {
-
- DefaultHttpClient httpclient = new DefaultHttpClient();
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope("localhost", 8080),
+ new UsernamePasswordCredentials("username", "password"));
+ CloseableHttpClient httpclient = HttpClients.custom()
+ .setCredentialsProvider(credsProvider).build();
try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope("localhost", 8080),
- new UsernamePasswordCredentials("username", "password"));
-
HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
HttpHost proxy = new HttpHost("localhost", 8080);
@@ -60,21 +63,21 @@ public class ClientProxyAuthentication {
System.out.println("via proxy: " + proxy);
System.out.println("to target: " + targetHost);
- HttpResponse response = httpclient.execute(targetHost, httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
+ CloseableHttpResponse response = httpclient.execute(targetHost, httpget);
+ try {
+ HttpEntity entity = response.getEntity();
+
+ System.out.println("----------------------------------------");
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
}
- EntityUtils.consume(entity);
-
} finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
+ httpclient.close();
}
}
}