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,28 +40,29 @@ 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());
|
||||||
|
HttpResponse response = httpclient.execute(httpget);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
System.out.println("executing request " + httpget.getURI());
|
System.out.println("----------------------------------------");
|
||||||
HttpResponse response = httpclient.execute(httpget);
|
System.out.println(response.getStatusLine());
|
||||||
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
|
}
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
// Do not feel like reading the response body
|
||||||
System.out.println(response.getStatusLine());
|
// Call abort on the request object
|
||||||
if (entity != null) {
|
httpget.abort();
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
} finally {
|
||||||
|
// When HttpClient instance is no longer needed,
|
||||||
|
// shut down the connection manager to ensure
|
||||||
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
|
|
||||||
// Do not feel like reading the response body
|
|
||||||
// Call abort on the request object
|
|
||||||
httpget.abort();
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,27 +41,28 @@ 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(
|
||||||
|
new AuthScope("localhost", 443),
|
||||||
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
HttpGet httpget = new HttpGet("https://localhost/protected");
|
||||||
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("executing request" + httpget.getRequestLine());
|
System.out.println("----------------------------------------");
|
||||||
HttpResponse response = httpclient.execute(httpget);
|
System.out.println(response.getStatusLine());
|
||||||
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
System.out.println("----------------------------------------");
|
}
|
||||||
System.out.println(response.getStatusLine());
|
EntityUtils.consume(entity);
|
||||||
if (entity != null) {
|
} finally {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
// When HttpClient instance is no longer needed,
|
||||||
|
// shut down the connection manager to ensure
|
||||||
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,40 +48,41 @@ 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" +
|
||||||
|
"/servlets-examples/servlet/RequestInfoExample");
|
||||||
|
|
||||||
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
File file = new File(args[0]);
|
||||||
"/servlets-examples/servlet/RequestInfoExample");
|
|
||||||
|
|
||||||
File file = new File(args[0]);
|
InputStreamEntity reqEntity = new InputStreamEntity(
|
||||||
|
new FileInputStream(file), -1);
|
||||||
|
reqEntity.setContentType("binary/octet-stream");
|
||||||
|
reqEntity.setChunked(true);
|
||||||
|
// It may be more appropriate to use FileEntity class in this particular
|
||||||
|
// instance but we are using a more generic InputStreamEntity to demonstrate
|
||||||
|
// the capability to stream out data from any arbitrary source
|
||||||
|
//
|
||||||
|
// FileEntity entity = new FileEntity(file, "binary/octet-stream");
|
||||||
|
|
||||||
InputStreamEntity reqEntity = new InputStreamEntity(
|
httppost.setEntity(reqEntity);
|
||||||
new FileInputStream(file), -1);
|
|
||||||
reqEntity.setContentType("binary/octet-stream");
|
|
||||||
reqEntity.setChunked(true);
|
|
||||||
// It may be more appropriate to use FileEntity class in this particular
|
|
||||||
// instance but we are using a more generic InputStreamEntity to demonstrate
|
|
||||||
// the capability to stream out data from any arbitrary source
|
|
||||||
//
|
|
||||||
// FileEntity entity = new FileEntity(file, "binary/octet-stream");
|
|
||||||
|
|
||||||
httppost.setEntity(reqEntity);
|
System.out.println("executing request " + httppost.getRequestLine());
|
||||||
|
HttpResponse response = httpclient.execute(httppost);
|
||||||
|
HttpEntity resEntity = response.getEntity();
|
||||||
|
|
||||||
System.out.println("executing request " + httppost.getRequestLine());
|
System.out.println("----------------------------------------");
|
||||||
HttpResponse response = httpclient.execute(httppost);
|
System.out.println(response.getStatusLine());
|
||||||
HttpEntity resEntity = response.getEntity();
|
if (resEntity != null) {
|
||||||
|
System.out.println("Response content length: " + resEntity.getContentLength());
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("Chunked?: " + resEntity.isChunked());
|
||||||
System.out.println(response.getStatusLine());
|
}
|
||||||
if (resEntity != null) {
|
EntityUtils.consume(resEntity);
|
||||||
System.out.println("Response content length: " + resEntity.getContentLength());
|
} finally {
|
||||||
System.out.println("Chunked?: " + resEntity.isChunked());
|
// When HttpClient instance is no longer needed,
|
||||||
|
// shut down the connection manager to ensure
|
||||||
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
EntityUtils.consume(resEntity);
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,48 +44,49 @@ 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
|
||||||
|
System.out.println("executing request " + httpget.getURI());
|
||||||
|
HttpResponse response = httpclient.execute(httpget);
|
||||||
|
|
||||||
// Execute HTTP request
|
System.out.println("----------------------------------------");
|
||||||
System.out.println("executing request " + httpget.getURI());
|
System.out.println(response.getStatusLine());
|
||||||
HttpResponse response = httpclient.execute(httpget);
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
// Get hold of the response entity
|
||||||
System.out.println(response.getStatusLine());
|
HttpEntity entity = response.getEntity();
|
||||||
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
|
|
||||||
instream.close();
|
|
||||||
|
|
||||||
|
// 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) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
} finally {
|
||||||
// shut down the connection manager to ensure
|
// When HttpClient instance is no longer needed,
|
||||||
// immediate deallocation of all system resources
|
// shut down the connection manager to ensure
|
||||||
httpclient.getConnectionManager().shutdown();
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,42 +52,44 @@ 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
|
||||||
|
CookieStore cookieStore = new BasicCookieStore();
|
||||||
|
|
||||||
// Create a local instance of cookie store
|
// Create local HTTP context
|
||||||
CookieStore cookieStore = new BasicCookieStore();
|
HttpContext localContext = new BasicHttpContext();
|
||||||
|
// Bind custom cookie store to the local context
|
||||||
|
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
|
||||||
|
|
||||||
// Create local HTTP context
|
HttpGet httpget = new HttpGet("http://www.google.com/");
|
||||||
HttpContext localContext = new BasicHttpContext();
|
|
||||||
// Bind custom cookie store to the local context
|
|
||||||
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
|
|
||||||
|
|
||||||
HttpGet httpget = new HttpGet("http://www.google.com/");
|
System.out.println("executing request " + httpget.getURI());
|
||||||
|
|
||||||
System.out.println("executing request " + httpget.getURI());
|
// Pass local context as a parameter
|
||||||
|
HttpResponse response = httpclient.execute(httpget, localContext);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
// Pass local context as a parameter
|
System.out.println("----------------------------------------");
|
||||||
HttpResponse response = httpclient.execute(httpget, localContext);
|
System.out.println(response.getStatusLine());
|
||||||
HttpEntity entity = response.getEntity();
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
// Consume response content
|
||||||
System.out.println(response.getStatusLine());
|
EntityUtils.consume(entity);
|
||||||
if (entity != null) {
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
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();
|
||||||
}
|
}
|
||||||
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("----------------------------------------");
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,37 +46,39 @@ 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();
|
||||||
|
|
||||||
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
||||||
FileInputStream instream = new FileInputStream(new File("my.keystore"));
|
|
||||||
try {
|
try {
|
||||||
trustStore.load(instream, "nopassword".toCharArray());
|
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();
|
||||||
|
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
System.out.println(response.getStatusLine());
|
||||||
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
|
}
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
instream.close();
|
// When HttpClient instance is no longer needed,
|
||||||
|
// shut down the connection manager to ensure
|
||||||
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
System.out.println(response.getStatusLine());
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
|
||||||
}
|
|
||||||
EntityUtils.consume(entity);
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,48 +48,50 @@ 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
|
||||||
|
String[] urisToGet = {
|
||||||
|
"http://jakarta.apache.org/",
|
||||||
|
"http://jakarta.apache.org/commons/",
|
||||||
|
"http://jakarta.apache.org/commons/httpclient/",
|
||||||
|
"http://svn.apache.org/viewvc/jakarta/httpcomponents/"
|
||||||
|
};
|
||||||
|
|
||||||
// create an array of URIs to perform GETs on
|
IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm);
|
||||||
String[] urisToGet = {
|
connEvictor.start();
|
||||||
"http://jakarta.apache.org/",
|
|
||||||
"http://jakarta.apache.org/commons/",
|
|
||||||
"http://jakarta.apache.org/commons/httpclient/",
|
|
||||||
"http://svn.apache.org/viewvc/jakarta/httpcomponents/"
|
|
||||||
};
|
|
||||||
|
|
||||||
IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm);
|
for (int i = 0; i < urisToGet.length; i++) {
|
||||||
connEvictor.start();
|
String requestURI = urisToGet[i];
|
||||||
|
HttpGet req = new HttpGet(requestURI);
|
||||||
|
|
||||||
for (int i = 0; i < urisToGet.length; i++) {
|
System.out.println("executing request " + requestURI);
|
||||||
String requestURI = urisToGet[i];
|
|
||||||
HttpGet req = new HttpGet(requestURI);
|
|
||||||
|
|
||||||
System.out.println("executing request " + requestURI);
|
HttpResponse rsp = httpclient.execute(req);
|
||||||
|
HttpEntity entity = rsp.getEntity();
|
||||||
|
|
||||||
HttpResponse rsp = httpclient.execute(req);
|
System.out.println("----------------------------------------");
|
||||||
HttpEntity entity = rsp.getEntity();
|
System.out.println(rsp.getStatusLine());
|
||||||
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
|
}
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
EntityUtils.consume(entity);
|
||||||
System.out.println(rsp.getStatusLine());
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
|
|
||||||
EntityUtils.consume(entity);
|
// Sleep 10 sec and let the connection evictor do its job
|
||||||
|
Thread.sleep(20000);
|
||||||
|
|
||||||
|
// Shut down the evictor thread
|
||||||
|
connEvictor.shutdown();
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sleep 10 sec and let the connection evictor do its job
|
|
||||||
Thread.sleep(20000);
|
|
||||||
|
|
||||||
// Shut down the evictor thread
|
|
||||||
connEvictor.shutdown();
|
|
||||||
connEvictor.join();
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class IdleConnectionEvictor extends Thread {
|
public static class IdleConnectionEvictor extends Thread {
|
||||||
|
|
|
@ -45,31 +45,33 @@ 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");
|
||||||
|
HttpGet req = new HttpGet("/");
|
||||||
|
|
||||||
HttpHost target = new HttpHost("www.apache.org", 80, "http");
|
System.out.println("executing request to " + target);
|
||||||
HttpGet req = new HttpGet("/");
|
|
||||||
|
|
||||||
System.out.println("executing request to " + target);
|
HttpResponse rsp = httpclient.execute(target, req);
|
||||||
|
HttpEntity entity = rsp.getEntity();
|
||||||
|
|
||||||
HttpResponse rsp = httpclient.execute(target, req);
|
System.out.println("----------------------------------------");
|
||||||
HttpEntity entity = rsp.getEntity();
|
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("----------------------------------------");
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
if (entity != null) {
|
||||||
System.out.println(rsp.getStatusLine());
|
System.out.println(EntityUtils.toString(entity));
|
||||||
Header[] headers = rsp.getAllHeaders();
|
}
|
||||||
for (int i = 0; i < headers.length; i++) {
|
|
||||||
System.out.println(headers[i]);
|
} finally {
|
||||||
|
// When HttpClient instance is no longer needed,
|
||||||
|
// shut down the connection manager to ensure
|
||||||
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println(EntityUtils.toString(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,31 +48,34 @@ 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();
|
||||||
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
|
try {
|
||||||
|
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");
|
||||||
HttpGet req = new HttpGet("/");
|
HttpGet req = new HttpGet("/");
|
||||||
|
|
||||||
System.out.println("executing request to " + target + " via " + proxy);
|
System.out.println("executing request to " + target + " via " + proxy);
|
||||||
HttpResponse rsp = httpclient.execute(target, req);
|
HttpResponse rsp = httpclient.execute(target, req);
|
||||||
HttpEntity entity = rsp.getEntity();
|
HttpEntity entity = rsp.getEntity();
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
System.out.println(rsp.getStatusLine());
|
System.out.println(rsp.getStatusLine());
|
||||||
Header[] headers = rsp.getAllHeaders();
|
Header[] headers = rsp.getAllHeaders();
|
||||||
for (int i = 0; i<headers.length; i++) {
|
for (int i = 0; i<headers.length; i++) {
|
||||||
System.out.println(headers[i]);
|
System.out.println(headers[i]);
|
||||||
|
}
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println(EntityUtils.toString(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -57,34 +57,37 @@ 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();
|
||||||
httpclient.getParams().setParameter("socks.host", "mysockshost");
|
try {
|
||||||
httpclient.getParams().setParameter("socks.port", 1234);
|
httpclient.getParams().setParameter("socks.host", "mysockshost");
|
||||||
httpclient.getConnectionManager().getSchemeRegistry().register(
|
httpclient.getParams().setParameter("socks.port", 1234);
|
||||||
new Scheme("http", 80, new MySchemeSocketFactory()));
|
httpclient.getConnectionManager().getSchemeRegistry().register(
|
||||||
|
new Scheme("http", 80, new MySchemeSocketFactory()));
|
||||||
|
|
||||||
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("/");
|
||||||
|
|
||||||
System.out.println("executing request to " + target + " via SOCKS proxy");
|
System.out.println("executing request to " + target + " via SOCKS proxy");
|
||||||
HttpResponse rsp = httpclient.execute(target, req);
|
HttpResponse rsp = httpclient.execute(target, req);
|
||||||
HttpEntity entity = rsp.getEntity();
|
HttpEntity entity = rsp.getEntity();
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
System.out.println(rsp.getStatusLine());
|
System.out.println(rsp.getStatusLine());
|
||||||
Header[] headers = rsp.getAllHeaders();
|
Header[] headers = rsp.getAllHeaders();
|
||||||
for (int i = 0; i<headers.length; i++) {
|
for (int i = 0; i<headers.length; i++) {
|
||||||
System.out.println(headers[i]);
|
System.out.println(headers[i]);
|
||||||
|
}
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println(EntityUtils.toString(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class MySchemeSocketFactory implements SchemeSocketFactory {
|
static class MySchemeSocketFactory implements SchemeSocketFactory {
|
||||||
|
|
|
@ -49,55 +49,57 @@ 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);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
HttpResponse response = httpclient.execute(httpget);
|
System.out.println("Login form get: " + response.getStatusLine());
|
||||||
HttpEntity entity = response.getEntity();
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
System.out.println("Login form get: " + response.getStatusLine());
|
System.out.println("Initial set of cookies:");
|
||||||
EntityUtils.consume(entity);
|
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
|
||||||
|
if (cookies.isEmpty()) {
|
||||||
System.out.println("Initial set of cookies:");
|
System.out.println("None");
|
||||||
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
|
} else {
|
||||||
if (cookies.isEmpty()) {
|
for (int i = 0; i < cookies.size(); i++) {
|
||||||
System.out.println("None");
|
System.out.println("- " + cookies.get(i).toString());
|
||||||
} else {
|
}
|
||||||
for (int i = 0; i < cookies.size(); i++) {
|
|
||||||
System.out.println("- " + cookies.get(i).toString());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
|
HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
|
||||||
"org=self_registered_users&" +
|
"org=self_registered_users&" +
|
||||||
"goto=/portal/dt&" +
|
"goto=/portal/dt&" +
|
||||||
"gotoOnFail=/portal/dt?error=true");
|
"gotoOnFail=/portal/dt?error=true");
|
||||||
|
|
||||||
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
|
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
|
||||||
nvps.add(new BasicNameValuePair("IDToken1", "username"));
|
nvps.add(new BasicNameValuePair("IDToken1", "username"));
|
||||||
nvps.add(new BasicNameValuePair("IDToken2", "password"));
|
nvps.add(new BasicNameValuePair("IDToken2", "password"));
|
||||||
|
|
||||||
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
|
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
|
||||||
|
|
||||||
response = httpclient.execute(httpost);
|
response = httpclient.execute(httpost);
|
||||||
entity = response.getEntity();
|
entity = response.getEntity();
|
||||||
|
|
||||||
System.out.println("Login form get: " + response.getStatusLine());
|
System.out.println("Login form get: " + response.getStatusLine());
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
System.out.println("Post logon cookies:");
|
System.out.println("Post logon cookies:");
|
||||||
cookies = httpclient.getCookieStore().getCookies();
|
cookies = httpclient.getCookieStore().getCookies();
|
||||||
if (cookies.isEmpty()) {
|
if (cookies.isEmpty()) {
|
||||||
System.out.println("None");
|
System.out.println("None");
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < cookies.size(); i++) {
|
for (int i = 0; i < cookies.size(); i++) {
|
||||||
System.out.println("- " + cookies.get(i).toString());
|
System.out.println("- " + cookies.get(i).toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
} finally {
|
||||||
// shut down the connection manager to ensure
|
// When HttpClient instance is no longer needed,
|
||||||
// immediate deallocation of all system resources
|
// shut down the connection manager to ensure
|
||||||
httpclient.getConnectionManager().shutdown();
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,65 +62,67 @@ 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(
|
||||||
|
final HttpRequest request,
|
||||||
public void process(
|
final HttpContext context) throws HttpException, IOException {
|
||||||
final HttpRequest request,
|
if (!request.containsHeader("Accept-Encoding")) {
|
||||||
final HttpContext context) throws HttpException, IOException {
|
request.addHeader("Accept-Encoding", "gzip");
|
||||||
if (!request.containsHeader("Accept-Encoding")) {
|
}
|
||||||
request.addHeader("Accept-Encoding", "gzip");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
|
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
|
||||||
|
|
||||||
public void process(
|
public void process(
|
||||||
final HttpResponse response,
|
final HttpResponse response,
|
||||||
final HttpContext context) throws HttpException, IOException {
|
final HttpContext context) throws HttpException, IOException {
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
Header ceheader = entity.getContentEncoding();
|
Header ceheader = entity.getContentEncoding();
|
||||||
if (ceheader != null) {
|
if (ceheader != null) {
|
||||||
HeaderElement[] codecs = ceheader.getElements();
|
HeaderElement[] codecs = ceheader.getElements();
|
||||||
for (int i = 0; i < codecs.length; i++) {
|
for (int i = 0; i < codecs.length; i++) {
|
||||||
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
|
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
|
||||||
response.setEntity(
|
response.setEntity(
|
||||||
new GzipDecompressingEntity(response.getEntity()));
|
new GzipDecompressingEntity(response.getEntity()));
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
System.out.println("Uncompressed size: "+content.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
} finally {
|
||||||
|
// When HttpClient instance is no longer needed,
|
||||||
HttpGet httpget = new HttpGet("http://www.apache.org/");
|
// shut down the connection manager to ensure
|
||||||
|
// immediate deallocation of all system resources
|
||||||
// Execute HTTP request
|
httpclient.getConnectionManager().shutdown();
|
||||||
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);
|
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
System.out.println("Uncompressed size: "+content.length());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GzipDecompressingEntity extends HttpEntityWrapper {
|
static class GzipDecompressingEntity extends HttpEntityWrapper {
|
||||||
|
|
|
@ -50,66 +50,68 @@ 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
|
||||||
|
HttpContext localContext = new BasicHttpContext();
|
||||||
|
|
||||||
// Create local execution context
|
HttpGet httpget = new HttpGet("http://localhost/test");
|
||||||
HttpContext localContext = new BasicHttpContext();
|
|
||||||
|
|
||||||
HttpGet httpget = new HttpGet("http://localhost/test");
|
boolean trying = true;
|
||||||
|
while (trying) {
|
||||||
|
System.out.println("executing request " + httpget.getRequestLine());
|
||||||
|
HttpResponse response = httpclient.execute(httpget, localContext);
|
||||||
|
|
||||||
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());
|
|
||||||
|
|
||||||
// Consume response content
|
|
||||||
HttpEntity entity = response.getEntity();
|
|
||||||
EntityUtils.consume(entity);
|
|
||||||
|
|
||||||
int sc = response.getStatusLine().getStatusCode();
|
|
||||||
|
|
||||||
AuthState authState = null;
|
|
||||||
if (sc == HttpStatus.SC_UNAUTHORIZED) {
|
|
||||||
// Target host authentication required
|
|
||||||
authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
|
|
||||||
}
|
|
||||||
if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
|
|
||||||
// Proxy authentication required
|
|
||||||
authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (authState != null) {
|
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
AuthScope authScope = authState.getAuthScope();
|
System.out.println(response.getStatusLine());
|
||||||
System.out.println("Please provide credentials");
|
|
||||||
System.out.println(" Host: " + authScope.getHost() + ":" + authScope.getPort());
|
// Consume response content
|
||||||
System.out.println(" Realm: " + authScope.getRealm());
|
HttpEntity entity = response.getEntity();
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
|
int sc = response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
|
AuthState authState = null;
|
||||||
|
if (sc == HttpStatus.SC_UNAUTHORIZED) {
|
||||||
|
// Target host authentication required
|
||||||
|
authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
|
||||||
|
}
|
||||||
|
if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
|
||||||
|
// Proxy authentication required
|
||||||
|
authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authState != null) {
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
AuthScope authScope = authState.getAuthScope();
|
||||||
|
System.out.println("Please provide credentials");
|
||||||
|
System.out.println(" Host: " + authScope.getHost() + ":" + authScope.getPort());
|
||||||
|
System.out.println(" Realm: " + authScope.getRealm());
|
||||||
|
|
||||||
|
|
||||||
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
System.out.print("Enter username: ");
|
System.out.print("Enter username: ");
|
||||||
String user = console.readLine();
|
String user = console.readLine();
|
||||||
System.out.print("Enter password: ");
|
System.out.print("Enter password: ");
|
||||||
String password = console.readLine();
|
String password = console.readLine();
|
||||||
|
|
||||||
if (user != null && user.length() > 0) {
|
if (user != null && user.length() > 0) {
|
||||||
Credentials creds = new UsernamePasswordCredentials(user, password);
|
Credentials creds = new UsernamePasswordCredentials(user, password);
|
||||||
httpclient.getCredentialsProvider().setCredentials(authScope, creds);
|
httpclient.getCredentialsProvider().setCredentials(authScope, creds);
|
||||||
trying = true;
|
trying = true;
|
||||||
|
} else {
|
||||||
|
trying = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
trying = false;
|
trying = false;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
trying = false;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
} finally {
|
||||||
// shut down the connection manager to ensure
|
// When HttpClient instance is no longer needed,
|
||||||
// immediate deallocation of all system resources
|
// shut down the connection manager to ensure
|
||||||
httpclient.getConnectionManager().shutdown();
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,48 +126,50 @@ 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();
|
||||||
|
// nsf.setStripPort(false);
|
||||||
|
// nsf.setSpengoGenerator(new BouncySpnegoTokenGenerator());
|
||||||
|
|
||||||
NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
|
httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
|
||||||
// nsf.setStripPort(false);
|
|
||||||
// nsf.setSpengoGenerator(new BouncySpnegoTokenGenerator());
|
|
||||||
|
|
||||||
httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
|
Credentials use_jaas_creds = new Credentials() {
|
||||||
|
|
||||||
Credentials use_jaas_creds = new Credentials() {
|
public String getPassword() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public Principal getUserPrincipal() {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
httpclient.getCredentialsProvider().setCredentials(
|
||||||
|
new AuthScope(null, -1, null),
|
||||||
|
use_jaas_creds);
|
||||||
|
|
||||||
|
HttpUriRequest request = new HttpGet("http://kerberoshost/");
|
||||||
|
HttpResponse response = httpclient.execute(request);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
System.out.println(response.getStatusLine());
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
if (entity != null) {
|
||||||
|
System.out.println(EntityUtils.toString(entity));
|
||||||
}
|
}
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
public Principal getUserPrincipal() {
|
// This ensures the connection gets released back to the manager
|
||||||
return null;
|
EntityUtils.consume(entity);
|
||||||
}
|
|
||||||
|
|
||||||
};
|
} finally {
|
||||||
|
// When HttpClient instance is no longer needed,
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
// shut down the connection manager to ensure
|
||||||
new AuthScope(null, -1, null),
|
// immediate deallocation of all system resources
|
||||||
use_jaas_creds);
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
|
||||||
HttpUriRequest request = new HttpGet("http://kerberoshost/");
|
|
||||||
HttpResponse response = httpclient.execute(request);
|
|
||||||
HttpEntity entity = response.getEntity();
|
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
System.out.println(response.getStatusLine());
|
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
if (entity != null) {
|
|
||||||
System.out.println(EntityUtils.toString(entity));
|
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
|
||||||
|
|
||||||
// This ensures the connection gets released back to the manager
|
|
||||||
EntityUtils.consume(entity);
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,37 +49,39 @@ 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
|
||||||
|
String[] urisToGet = {
|
||||||
|
"http://hc.apache.org/",
|
||||||
|
"http://hc.apache.org/httpcomponents-core-ga/",
|
||||||
|
"http://hc.apache.org/httpcomponents-client-ga/",
|
||||||
|
"http://svn.apache.org/viewvc/httpcomponents/"
|
||||||
|
};
|
||||||
|
|
||||||
// create an array of URIs to perform GETs on
|
// create a thread for each URI
|
||||||
String[] urisToGet = {
|
GetThread[] threads = new GetThread[urisToGet.length];
|
||||||
"http://hc.apache.org/",
|
for (int i = 0; i < threads.length; i++) {
|
||||||
"http://hc.apache.org/httpcomponents-core-ga/",
|
HttpGet httpget = new HttpGet(urisToGet[i]);
|
||||||
"http://hc.apache.org/httpcomponents-client-ga/",
|
threads[i] = new GetThread(httpclient, httpget, i + 1);
|
||||||
"http://svn.apache.org/viewvc/httpcomponents/"
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// create a thread for each URI
|
// start the threads
|
||||||
GetThread[] threads = new GetThread[urisToGet.length];
|
for (int j = 0; j < threads.length; j++) {
|
||||||
for (int i = 0; i < threads.length; i++) {
|
threads[j].start();
|
||||||
HttpGet httpget = new HttpGet(urisToGet[i]);
|
}
|
||||||
threads[i] = new GetThread(httpClient, httpget, i + 1);
|
|
||||||
|
// join the threads
|
||||||
|
for (int j = 0; j < threads.length; j++) {
|
||||||
|
threads[j].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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the threads
|
|
||||||
for (int j = 0; j < threads.length; j++) {
|
|
||||||
threads[j].start();
|
|
||||||
}
|
|
||||||
|
|
||||||
// join the threads
|
|
||||||
for (int j = 0; j < threads.length; j++) {
|
|
||||||
threads[j].join();
|
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpClient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -54,43 +54,45 @@ 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(
|
||||||
|
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
||||||
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
// Create AuthCache instance
|
||||||
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
AuthCache authCache = new BasicAuthCache();
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
// Generate BASIC scheme object and add it to the local
|
||||||
|
// auth cache
|
||||||
|
BasicScheme basicAuth = new BasicScheme();
|
||||||
|
authCache.put(targetHost, basicAuth);
|
||||||
|
|
||||||
// Create AuthCache instance
|
// Add AuthCache to the execution context
|
||||||
AuthCache authCache = new BasicAuthCache();
|
BasicHttpContext localcontext = new BasicHttpContext();
|
||||||
// Generate BASIC scheme object and add it to the local
|
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||||
// auth cache
|
|
||||||
BasicScheme basicAuth = new BasicScheme();
|
|
||||||
authCache.put(targetHost, basicAuth);
|
|
||||||
|
|
||||||
// Add AuthCache to the execution context
|
HttpGet httpget = new HttpGet("/");
|
||||||
BasicHttpContext localcontext = new BasicHttpContext();
|
|
||||||
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
|
||||||
|
|
||||||
HttpGet httpget = new HttpGet("/");
|
System.out.println("executing request: " + httpget.getRequestLine());
|
||||||
|
System.out.println("to target: " + targetHost);
|
||||||
|
|
||||||
System.out.println("executing request: " + httpget.getRequestLine());
|
for (int i = 0; i < 3; i++) {
|
||||||
System.out.println("to target: " + targetHost);
|
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
System.out.println("----------------------------------------");
|
||||||
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
System.out.println(response.getStatusLine());
|
||||||
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
System.out.println("----------------------------------------");
|
}
|
||||||
System.out.println(response.getStatusLine());
|
EntityUtils.consume(entity);
|
||||||
if (entity != null) {
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
} finally {
|
||||||
// shut down the connection manager to ensure
|
// When HttpClient instance is no longer needed,
|
||||||
// immediate deallocation of all system resources
|
// shut down the connection manager to ensure
|
||||||
httpclient.getConnectionManager().shutdown();
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,47 +54,49 @@ 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(
|
||||||
|
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
||||||
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
// Create AuthCache instance
|
||||||
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
AuthCache authCache = new BasicAuthCache();
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
// Generate DIGEST scheme object, initialize it and add it to the local
|
||||||
|
// auth cache
|
||||||
|
DigestScheme digestAuth = new DigestScheme();
|
||||||
|
// Suppose we already know the realm name
|
||||||
|
digestAuth.overrideParamter("realm", "some realm");
|
||||||
|
// Suppose we already know the expected nonce value
|
||||||
|
digestAuth.overrideParamter("nonce", "whatever");
|
||||||
|
authCache.put(targetHost, digestAuth);
|
||||||
|
|
||||||
// Create AuthCache instance
|
// Add AuthCache to the execution context
|
||||||
AuthCache authCache = new BasicAuthCache();
|
BasicHttpContext localcontext = new BasicHttpContext();
|
||||||
// Generate DIGEST scheme object, initialize it and add it to the local
|
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||||
// auth cache
|
|
||||||
DigestScheme digestAuth = new DigestScheme();
|
|
||||||
// Suppose we already know the realm name
|
|
||||||
digestAuth.overrideParamter("realm", "some realm");
|
|
||||||
// Suppose we already know the expected nonce value
|
|
||||||
digestAuth.overrideParamter("nonce", "whatever");
|
|
||||||
authCache.put(targetHost, digestAuth);
|
|
||||||
|
|
||||||
// Add AuthCache to the execution context
|
HttpGet httpget = new HttpGet("/");
|
||||||
BasicHttpContext localcontext = new BasicHttpContext();
|
|
||||||
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
|
||||||
|
|
||||||
HttpGet httpget = new HttpGet("/");
|
System.out.println("executing request: " + httpget.getRequestLine());
|
||||||
|
System.out.println("to target: " + targetHost);
|
||||||
|
|
||||||
System.out.println("executing request: " + httpget.getRequestLine());
|
for (int i = 0; i < 3; i++) {
|
||||||
System.out.println("to target: " + targetHost);
|
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
System.out.println("----------------------------------------");
|
||||||
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
System.out.println(response.getStatusLine());
|
||||||
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
System.out.println("----------------------------------------");
|
}
|
||||||
System.out.println(response.getStatusLine());
|
EntityUtils.consume(entity);
|
||||||
if (entity != null) {
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
} finally {
|
||||||
// shut down the connection manager to ensure
|
// When HttpClient instance is no longer needed,
|
||||||
// immediate deallocation of all system resources
|
// shut down the connection manager to ensure
|
||||||
httpclient.getConnectionManager().shutdown();
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,35 +44,37 @@ 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(
|
||||||
|
new AuthScope("localhost", 8080),
|
||||||
|
new UsernamePasswordCredentials("username", "password"));
|
||||||
|
|
||||||
httpclient.getCredentialsProvider().setCredentials(
|
HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
|
||||||
new AuthScope("localhost", 8080),
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
new UsernamePasswordCredentials("username", "password"));
|
|
||||||
|
|
||||||
HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
|
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
|
||||||
HttpHost proxy = new HttpHost("localhost", 8080);
|
|
||||||
|
|
||||||
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
|
HttpGet httpget = new HttpGet("/");
|
||||||
|
|
||||||
HttpGet httpget = new HttpGet("/");
|
System.out.println("executing request: " + httpget.getRequestLine());
|
||||||
|
System.out.println("via proxy: " + proxy);
|
||||||
|
System.out.println("to target: " + targetHost);
|
||||||
|
|
||||||
System.out.println("executing request: " + httpget.getRequestLine());
|
HttpResponse response = httpclient.execute(targetHost, httpget);
|
||||||
System.out.println("via proxy: " + proxy);
|
HttpEntity entity = response.getEntity();
|
||||||
System.out.println("to target: " + targetHost);
|
|
||||||
|
|
||||||
HttpResponse response = httpclient.execute(targetHost, httpget);
|
System.out.println("----------------------------------------");
|
||||||
HttpEntity entity = response.getEntity();
|
System.out.println(response.getStatusLine());
|
||||||
|
if (entity != null) {
|
||||||
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
|
}
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
} finally {
|
||||||
System.out.println(response.getStatusLine());
|
// When HttpClient instance is no longer needed,
|
||||||
if (entity != null) {
|
// shut down the connection manager to ensure
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
// immediate deallocation of all system resources
|
||||||
|
httpclient.getConnectionManager().shutdown();
|
||||||
}
|
}
|
||||||
EntityUtils.consume(entity);
|
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,22 +42,24 @@ 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());
|
// Create a response handler
|
||||||
|
ResponseHandler<String> responseHandler = new BasicResponseHandler();
|
||||||
|
String responseBody = httpclient.execute(httpget, responseHandler);
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
System.out.println(responseBody);
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
// Create a response handler
|
} finally {
|
||||||
ResponseHandler<String> responseHandler = new BasicResponseHandler();
|
// When HttpClient instance is no longer needed,
|
||||||
String responseBody = httpclient.execute(httpget, responseHandler);
|
// shut down the connection manager to ensure
|
||||||
System.out.println("----------------------------------------");
|
// immediate deallocation of all system resources
|
||||||
System.out.println(responseBody);
|
httpclient.getConnectionManager().shutdown();
|
||||||
System.out.println("----------------------------------------");
|
}
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,29 +49,32 @@ 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" +
|
||||||
|
"/servlets-examples/servlet/RequestInfoExample");
|
||||||
|
|
||||||
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
FileBody bin = new FileBody(new File(args[0]));
|
||||||
"/servlets-examples/servlet/RequestInfoExample");
|
StringBody comment = new StringBody("A binary file of some kind");
|
||||||
|
|
||||||
FileBody bin = new FileBody(new File(args[0]));
|
MultipartEntity reqEntity = new MultipartEntity();
|
||||||
StringBody comment = new StringBody("A binary file of some kind");
|
reqEntity.addPart("bin", bin);
|
||||||
|
reqEntity.addPart("comment", comment);
|
||||||
|
|
||||||
MultipartEntity reqEntity = new MultipartEntity();
|
httppost.setEntity(reqEntity);
|
||||||
reqEntity.addPart("bin", bin);
|
|
||||||
reqEntity.addPart("comment", comment);
|
|
||||||
|
|
||||||
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("executing request " + httppost.getRequestLine());
|
||||||
System.out.println(response.getStatusLine());
|
HttpResponse response = httpclient.execute(httppost);
|
||||||
if (resEntity != null) {
|
HttpEntity resEntity = response.getEntity();
|
||||||
System.out.println("Response content length: " + resEntity.getContentLength());
|
|
||||||
|
System.out.println("----------------------------------------");
|
||||||
|
System.out.println(response.getStatusLine());
|
||||||
|
if (resEntity != null) {
|
||||||
|
System.out.println("Response content length: " + resEntity.getContentLength());
|
||||||
|
}
|
||||||
|
EntityUtils.consume(resEntity);
|
||||||
|
} finally {
|
||||||
|
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
|
||||||
}
|
}
|
||||||
EntityUtils.consume(resEntity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue