Fixed examples
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1026120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6b539c8c86
commit
e8bb17f4e2
|
@ -31,6 +31,7 @@ import org.apache.http.auth.AuthScope;
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple example that uses HttpClient to execute an HTTP request against
|
* A simple example that uses HttpClient to execute an HTTP request against
|
||||||
|
@ -56,9 +57,7 @@ public class ClientAuthentication {
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
}
|
}
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -35,6 +35,7 @@ import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.entity.InputStreamEntity;
|
import org.apache.http.entity.InputStreamEntity;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example how to use unbuffered chunk-encoded POST request.
|
* Example how to use unbuffered chunk-encoded POST request.
|
||||||
|
@ -75,9 +76,7 @@ public class ClientChunkEncodedPost {
|
||||||
System.out.println("Response content length: " + resEntity.getContentLength());
|
System.out.println("Response content length: " + resEntity.getContentLength());
|
||||||
System.out.println("Chunked?: " + resEntity.isChunked());
|
System.out.println("Chunked?: " + resEntity.isChunked());
|
||||||
}
|
}
|
||||||
if (resEntity != null) {
|
EntityUtils.consume(resEntity);
|
||||||
resEntity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.apache.http.impl.client.BasicCookieStore;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,9 +80,7 @@ public class ClientCustomContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Consume response content
|
// Consume response content
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.conn.scheme.Scheme;
|
import org.apache.http.conn.scheme.Scheme;
|
||||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This example demonstrates how to create secure connections with a custom SSL
|
* This example demonstrates how to create secure connections with a custom SSL
|
||||||
|
@ -70,9 +71,7 @@ public class ClientCustomSSL {
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
}
|
}
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -38,6 +38,7 @@ import org.apache.http.conn.scheme.Scheme;
|
||||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example demonstrating how to evict expired and idle connections
|
* Example demonstrating how to evict expired and idle connections
|
||||||
|
@ -52,7 +53,7 @@ public class ClientEvictExpiredConnections {
|
||||||
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
|
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
|
||||||
|
|
||||||
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
|
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
|
||||||
cm.setMaxTotalConnections(100);
|
cm.setMaxTotal(100);
|
||||||
|
|
||||||
HttpClient httpclient = new DefaultHttpClient(cm);
|
HttpClient httpclient = new DefaultHttpClient(cm);
|
||||||
|
|
||||||
|
@ -83,9 +84,7 @@ public class ClientEvictExpiredConnections {
|
||||||
}
|
}
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sleep 10 sec and let the connection evictor do its job
|
// Sleep 10 sec and let the connection evictor do its job
|
||||||
|
|
|
@ -38,6 +38,7 @@ import org.apache.http.cookie.Cookie;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.apache.http.protocol.HTTP;
|
import org.apache.http.protocol.HTTP;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A example that demonstrates how HttpClient APIs can be used to perform
|
* A example that demonstrates how HttpClient APIs can be used to perform
|
||||||
|
@ -55,9 +56,8 @@ public class ClientFormLogin {
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
|
|
||||||
System.out.println("Login form get: " + response.getStatusLine());
|
System.out.println("Login form get: " + response.getStatusLine());
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
System.out.println("Initial set of cookies:");
|
System.out.println("Initial set of cookies:");
|
||||||
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
|
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
|
||||||
if (cookies.isEmpty()) {
|
if (cookies.isEmpty()) {
|
||||||
|
@ -83,9 +83,7 @@ public class ClientFormLogin {
|
||||||
entity = response.getEntity();
|
entity = response.getEntity();
|
||||||
|
|
||||||
System.out.println("Login form get: " + response.getStatusLine());
|
System.out.println("Login form get: " + response.getStatusLine());
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Post logon cookies:");
|
System.out.println("Post logon cookies:");
|
||||||
cookies = httpclient.getCookieStore().getCookies();
|
cookies = httpclient.getCookieStore().getCookies();
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.apache.http.client.protocol.ClientContext;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple example that uses HttpClient to execute an HTTP request against
|
* A simple example that uses HttpClient to execute an HTTP request against
|
||||||
|
@ -65,9 +66,7 @@ public class ClientInteractiveAuthentication {
|
||||||
|
|
||||||
// Consume response content
|
// Consume response content
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
int sc = response.getStatusLine().getStatusCode();
|
int sc = response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
|
|
|
@ -162,9 +162,7 @@ public class ClientKerberosAuthentication {
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
|
|
||||||
// This ensures the connection gets released back to the manager
|
// This ensures the connection gets released back to the manager
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -55,7 +55,7 @@ public class ClientMultiThreadedExecution {
|
||||||
// This connection manager must be used if more than one thread will
|
// This connection manager must be used if more than one thread will
|
||||||
// be using the HttpClient.
|
// be using the HttpClient.
|
||||||
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
|
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
|
||||||
cm.setMaxTotalConnections(100);
|
cm.setMaxTotal(100);
|
||||||
|
|
||||||
HttpClient httpClient = new DefaultHttpClient(cm);
|
HttpClient httpClient = new DefaultHttpClient(cm);
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ import org.apache.http.impl.auth.BasicScheme;
|
||||||
import org.apache.http.impl.client.BasicAuthCache;
|
import org.apache.http.impl.client.BasicAuthCache;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An example of HttpClient can be customized to authenticate
|
* An example of HttpClient can be customized to authenticate
|
||||||
|
@ -82,8 +83,8 @@ public class ClientPreemptiveBasicAuthentication {
|
||||||
System.out.println(response.getStatusLine());
|
System.out.println(response.getStatusLine());
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
entity.consumeContent();
|
|
||||||
}
|
}
|
||||||
|
EntityUtils.consume(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
|
|
|
@ -37,6 +37,7 @@ import org.apache.http.impl.auth.DigestScheme;
|
||||||
import org.apache.http.impl.client.BasicAuthCache;
|
import org.apache.http.impl.client.BasicAuthCache;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An example of HttpClient can be customized to authenticate
|
* An example of HttpClient can be customized to authenticate
|
||||||
|
@ -86,8 +87,8 @@ public class ClientPreemptiveDigestAuthentication {
|
||||||
System.out.println(response.getStatusLine());
|
System.out.println(response.getStatusLine());
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
entity.consumeContent();
|
|
||||||
}
|
}
|
||||||
|
EntityUtils.consume(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When HttpClient instance is no longer needed,
|
// When HttpClient instance is no longer needed,
|
||||||
|
|
|
@ -33,6 +33,7 @@ import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.conn.params.ConnRoutePNames;
|
import org.apache.http.conn.params.ConnRoutePNames;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple example that uses HttpClient to execute an HTTP request
|
* A simple example that uses HttpClient to execute an HTTP request
|
||||||
|
@ -67,9 +68,7 @@ public class ClientProxyAuthentication {
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
System.out.println("Response content length: " + entity.getContentLength());
|
||||||
}
|
}
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
Loading…
Reference in New Issue