HTTPCLIENT-1026: shut down connection manager in a try-finally
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1055629 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
441006d804
commit
a703914575
|
@ -40,7 +40,7 @@ public class ClientAbortMethod {
|
||||||
|
|
||||||
public final static void main(String[] args) throws Exception {
|
public final static void main(String[] args) throws Exception {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpGet httpget = new HttpGet("http://www.apache.org/");
|
HttpGet httpget = new HttpGet("http://www.apache.org/");
|
||||||
|
|
||||||
System.out.println("executing request " + httpget.getURI());
|
System.out.println("executing request " + httpget.getURI());
|
||||||
|
@ -57,12 +57,13 @@ public class ClientAbortMethod {
|
||||||
// Do not feel like reading the response body
|
// Do not feel like reading the response body
|
||||||
// Call abort on the request object
|
// Call abort on the request object
|
||||||
httpget.abort();
|
httpget.abort();
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class ClientAuthentication {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
httpclient.getCredentialsProvider().setCredentials(
|
||||||
new AuthScope("localhost", 443),
|
new AuthScope("localhost", 443),
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
@ -58,10 +58,11 @@ public class ClientAuthentication {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class ClientChunkEncodedPost {
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
||||||
"/servlets-examples/servlet/RequestInfoExample");
|
"/servlets-examples/servlet/RequestInfoExample");
|
||||||
|
|
||||||
|
@ -77,11 +77,12 @@ public class ClientChunkEncodedPost {
|
||||||
System.out.println("Chunked?: " + resEntity.isChunked());
|
System.out.println("Chunked?: " + resEntity.isChunked());
|
||||||
}
|
}
|
||||||
EntityUtils.consume(resEntity);
|
EntityUtils.consume(resEntity);
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class ClientConnectionRelease {
|
||||||
|
|
||||||
public final static void main(String[] args) throws Exception {
|
public final static void main(String[] args) throws Exception {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpGet httpget = new HttpGet("http://www.apache.org/");
|
HttpGet httpget = new HttpGet("http://www.apache.org/");
|
||||||
|
|
||||||
// Execute HTTP request
|
// Execute HTTP request
|
||||||
|
@ -77,16 +77,17 @@ public class ClientConnectionRelease {
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
// Closing the input stream will trigger connection release
|
// Closing the input stream will trigger connection release
|
||||||
instream.close();
|
try { instream.close(); } catch (Exception ignore) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class ClientCustomContext {
|
||||||
public final static void main(String[] args) throws Exception {
|
public final static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
// Create a local instance of cookie store
|
// Create a local instance of cookie store
|
||||||
CookieStore cookieStore = new BasicCookieStore();
|
CookieStore cookieStore = new BasicCookieStore();
|
||||||
|
|
||||||
|
@ -84,11 +84,13 @@ public class ClientCustomContext {
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,13 +46,13 @@ public class ClientCustomSSL {
|
||||||
|
|
||||||
public final static void main(String[] args) throws Exception {
|
public final static void main(String[] args) throws Exception {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
FileInputStream instream = new FileInputStream(new File("my.keystore"));
|
FileInputStream instream = new FileInputStream(new File("my.keystore"));
|
||||||
try {
|
try {
|
||||||
trustStore.load(instream, "nopassword".toCharArray());
|
trustStore.load(instream, "nopassword".toCharArray());
|
||||||
} finally {
|
} finally {
|
||||||
instream.close();
|
try { instream.close(); } catch (Exception ignore) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
|
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
|
||||||
|
@ -73,10 +73,12 @@ public class ClientCustomSSL {
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class ClientEvictExpiredConnections {
|
||||||
cm.setMaxTotal(100);
|
cm.setMaxTotal(100);
|
||||||
|
|
||||||
HttpClient httpclient = new DefaultHttpClient(cm);
|
HttpClient httpclient = new DefaultHttpClient(cm);
|
||||||
|
try {
|
||||||
// create an array of URIs to perform GETs on
|
// create an array of URIs to perform GETs on
|
||||||
String[] urisToGet = {
|
String[] urisToGet = {
|
||||||
"http://jakarta.apache.org/",
|
"http://jakarta.apache.org/",
|
||||||
|
@ -86,11 +86,13 @@ public class ClientEvictExpiredConnections {
|
||||||
connEvictor.shutdown();
|
connEvictor.shutdown();
|
||||||
connEvictor.join();
|
connEvictor.join();
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class IdleConnectionEvictor extends Thread {
|
public static class IdleConnectionEvictor extends Thread {
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class ClientExecuteDirect {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpHost target = new HttpHost("www.apache.org", 80, "http");
|
HttpHost target = new HttpHost("www.apache.org", 80, "http");
|
||||||
HttpGet req = new HttpGet("/");
|
HttpGet req = new HttpGet("/");
|
||||||
|
|
||||||
|
@ -66,10 +66,12 @@ public class ClientExecuteDirect {
|
||||||
System.out.println(EntityUtils.toString(entity));
|
System.out.println(EntityUtils.toString(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ public class ClientExecuteProxy {
|
||||||
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
|
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
|
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
|
||||||
|
|
||||||
HttpHost target = new HttpHost("issues.apache.org", 443, "https");
|
HttpHost target = new HttpHost("issues.apache.org", 443, "https");
|
||||||
|
@ -69,10 +70,12 @@ public class ClientExecuteProxy {
|
||||||
System.out.println(EntityUtils.toString(entity));
|
System.out.println(EntityUtils.toString(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -57,6 +57,7 @@ public class ClientExecuteSOCKS {
|
||||||
|
|
||||||
public static void main(String[] args)throws Exception {
|
public static void main(String[] args)throws Exception {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.getParams().setParameter("socks.host", "mysockshost");
|
httpclient.getParams().setParameter("socks.host", "mysockshost");
|
||||||
httpclient.getParams().setParameter("socks.port", 1234);
|
httpclient.getParams().setParameter("socks.port", 1234);
|
||||||
httpclient.getConnectionManager().getSchemeRegistry().register(
|
httpclient.getConnectionManager().getSchemeRegistry().register(
|
||||||
|
@ -81,11 +82,13 @@ public class ClientExecuteSOCKS {
|
||||||
System.out.println(EntityUtils.toString(entity));
|
System.out.println(EntityUtils.toString(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class MySchemeSocketFactory implements SchemeSocketFactory {
|
static class MySchemeSocketFactory implements SchemeSocketFactory {
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class ClientFormLogin {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");
|
HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");
|
||||||
|
|
||||||
HttpResponse response = httpclient.execute(httpget);
|
HttpResponse response = httpclient.execute(httpget);
|
||||||
|
@ -95,9 +95,11 @@ public class ClientFormLogin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ public class ClientGZipContentCompression {
|
||||||
|
|
||||||
public final static void main(String[] args) throws Exception {
|
public final static void main(String[] args) throws Exception {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
|
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
|
||||||
|
|
||||||
public void process(
|
public void process(
|
||||||
|
@ -117,11 +117,13 @@ public class ClientGZipContentCompression {
|
||||||
System.out.println("Uncompressed size: "+content.length());
|
System.out.println("Uncompressed size: "+content.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class GzipDecompressingEntity extends HttpEntityWrapper {
|
static class GzipDecompressingEntity extends HttpEntityWrapper {
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ public class ClientInteractiveAuthentication {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
// Create local execution context
|
// Create local execution context
|
||||||
HttpContext localContext = new BasicHttpContext();
|
HttpContext localContext = new BasicHttpContext();
|
||||||
|
|
||||||
|
@ -107,9 +107,11 @@ public class ClientInteractiveAuthentication {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,7 +126,7 @@ public class ClientKerberosAuthentication {
|
||||||
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
|
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
|
NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
|
||||||
// nsf.setStripPort(false);
|
// nsf.setStripPort(false);
|
||||||
// nsf.setSpengoGenerator(new BouncySpnegoTokenGenerator());
|
// nsf.setSpengoGenerator(new BouncySpnegoTokenGenerator());
|
||||||
|
@ -164,10 +164,12 @@ public class ClientKerberosAuthentication {
|
||||||
// This ensures the connection gets released back to the manager
|
// This ensures the connection gets released back to the manager
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,8 +49,8 @@ public class ClientMultiThreadedExecution {
|
||||||
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
|
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
|
||||||
cm.setMaxTotal(100);
|
cm.setMaxTotal(100);
|
||||||
|
|
||||||
HttpClient httpClient = new DefaultHttpClient(cm);
|
HttpClient httpclient = new DefaultHttpClient(cm);
|
||||||
|
try {
|
||||||
// create an array of URIs to perform GETs on
|
// create an array of URIs to perform GETs on
|
||||||
String[] urisToGet = {
|
String[] urisToGet = {
|
||||||
"http://hc.apache.org/",
|
"http://hc.apache.org/",
|
||||||
|
@ -63,7 +63,7 @@ public class ClientMultiThreadedExecution {
|
||||||
GetThread[] threads = new GetThread[urisToGet.length];
|
GetThread[] threads = new GetThread[urisToGet.length];
|
||||||
for (int i = 0; i < threads.length; i++) {
|
for (int i = 0; i < threads.length; i++) {
|
||||||
HttpGet httpget = new HttpGet(urisToGet[i]);
|
HttpGet httpget = new HttpGet(urisToGet[i]);
|
||||||
threads[i] = new GetThread(httpClient, httpget, i + 1);
|
threads[i] = new GetThread(httpclient, httpget, i + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the threads
|
// start the threads
|
||||||
|
@ -76,10 +76,12 @@ public class ClientMultiThreadedExecution {
|
||||||
threads[j].join();
|
threads[j].join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpClient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class ClientPreemptiveBasicAuthentication {
|
||||||
HttpHost targetHost = new HttpHost("localhost", 80, "http");
|
HttpHost targetHost = new HttpHost("localhost", 80, "http");
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
httpclient.getCredentialsProvider().setCredentials(
|
||||||
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
@ -87,10 +87,12 @@ public class ClientPreemptiveBasicAuthentication {
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class ClientPreemptiveDigestAuthentication {
|
||||||
HttpHost targetHost = new HttpHost("localhost", 80, "http");
|
HttpHost targetHost = new HttpHost("localhost", 80, "http");
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
httpclient.getCredentialsProvider().setCredentials(
|
||||||
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
@ -91,10 +91,12 @@ public class ClientPreemptiveDigestAuthentication {
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class ClientProxyAuthentication {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
httpclient.getCredentialsProvider().setCredentials(
|
||||||
new AuthScope("localhost", 8080),
|
new AuthScope("localhost", 8080),
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
@ -70,9 +70,11 @@ public class ClientProxyAuthentication {
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class ClientWithResponseHandler {
|
||||||
public final static void main(String[] args) throws Exception {
|
public final static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpGet httpget = new HttpGet("http://www.google.com/");
|
HttpGet httpget = new HttpGet("http://www.google.com/");
|
||||||
|
|
||||||
System.out.println("executing request " + httpget.getURI());
|
System.out.println("executing request " + httpget.getURI());
|
||||||
|
@ -54,11 +54,13 @@ public class ClientWithResponseHandler {
|
||||||
System.out.println(responseBody);
|
System.out.println(responseBody);
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
|
} finally {
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
// shut down the connection manager to ensure
|
// shut down the connection manager to ensure
|
||||||
// immediate deallocation of all system resources
|
// immediate deallocation of all system resources
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class ClientMultipartFormPost {
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
try {
|
||||||
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
||||||
"/servlets-examples/servlet/RequestInfoExample");
|
"/servlets-examples/servlet/RequestInfoExample");
|
||||||
|
|
||||||
|
@ -72,6 +72,9 @@ public class ClientMultipartFormPost {
|
||||||
System.out.println("Response content length: " + resEntity.getContentLength());
|
System.out.println("Response content length: " + resEntity.getContentLength());
|
||||||
}
|
}
|
||||||
EntityUtils.consume(resEntity);
|
EntityUtils.consume(resEntity);
|
||||||
|
} finally {
|
||||||
|
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue