From b2ad050679397d6b1868916d98fb4604a75ddcc5 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 3 Nov 2010 16:09:07 +0000 Subject: [PATCH] Updated HttpClient tutorial for the 4.1-beta1 release git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1030511 13f79535-47bb-0310-9956-ffa450edef68 --- src/docbkx/advanced.xml | 12 ++----- src/docbkx/authentication.xml | 60 +++++++++++++++-------------------- src/docbkx/caching.xml | 50 ++++++++++++++++++----------- src/docbkx/connmgmt.xml | 9 ++---- src/docbkx/fundamentals.xml | 7 ++-- 5 files changed, 65 insertions(+), 73 deletions(-) diff --git a/src/docbkx/advanced.xml b/src/docbkx/advanced.xml index 5f6a07304..c98d4f239 100644 --- a/src/docbkx/advanced.xml +++ b/src/docbkx/advanced.xml @@ -181,9 +181,7 @@ HttpContext localContext = new BasicHttpContext(); HttpGet httpget = new HttpGet("http://localhost:8080/"); HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); -if (entity != null) { - entity.consumeContent(); -} +EntityUtils.consume(entity); Object userToken = localContext.getAttribute(ClientContext.USER_TOKEN); System.out.println(userToken); ]]> @@ -200,9 +198,7 @@ HttpContext localContext1 = new BasicHttpContext(); HttpGet httpget1 = new HttpGet("http://localhost:8080/"); HttpResponse response1 = httpclient.execute(httpget1, localContext1); HttpEntity entity1 = response1.getEntity(); -if (entity1 != null) { - entity1.consumeContent(); -} +EntityUtils.consume(entity1); Principal principal = (Principal) localContext1.getAttribute( ClientContext.USER_TOKEN); @@ -211,9 +207,7 @@ localContext2.setAttribute(ClientContext.USER_TOKEN, principal); HttpGet httpget2 = new HttpGet("http://localhost:8080/"); HttpResponse response2 = httpclient.execute(httpget2, localContext2); HttpEntity entity2 = response2.getEntity(); -if (entity2 != null) { - entity2.consumeContent(); -} +EntityUtils.consume(entity2); ]]> diff --git a/src/docbkx/authentication.xml b/src/docbkx/authentication.xml index 396f1e92b..69e5ef4b5 100644 --- a/src/docbkx/authentication.xml +++ b/src/docbkx/authentication.xml @@ -24,9 +24,8 @@ HTTP authentication HttpClient provides full support for authentication schemes defined by the HTTP standard - specification. HttpClient's authentication framework can also be extended to support - non-standard authentication schemes such as NTLM and - SPNEGO. + specification as well as a number of widely used non-standard authentication schemes such + as NTLM and SPNEGO.
User credentials Any process of user authentication requires a set of credentials that can be used to @@ -109,9 +108,7 @@ pwd NTLM: NTLM is a proprietary authentication scheme developed by Microsoft and optimized for Windows platforms. NTLM is believed to be more secure than - Digest. This scheme is requires an external NTLM engine to be functional. - For details please refer to the NTLM_SUPPORT.txt document - included with HttpClient distributions. + Digest. @@ -194,23 +191,28 @@ httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); Basic: - Basic authentication scheme + Basic authentication Digest: - Digest authentication scheme + Digest authentication + + + + + NTLM: + NTLMv1, NTLMv2, and NTLM2 Session authentication + + + + + SPNEGO: + SPNEGO/Kerberos authentication - Please note NTLM and SPNEGO schemes are - NOT registered per default. The NTLM cannot - be enabled per default due to licensing and legal reasons. For details on how to - enable NTLM support please see this - section. SPNEGO setup tends to be system specific and must be - properly configured in order to be functional. See this - section for details.
Credentials provider @@ -376,23 +378,18 @@ HttpGet httpget = new HttpGet("/"); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); } ]]>
NTLM Authentication - Currently HttpClient does not provide support for the NTLM authentication scheme out - of the box and probably never will. The reasons for that are legal rather than - technical. However, NTLM authentication can be enabled by using an external + As of version 4.1 HttpClient provides full support for NTLMv1, NTLMv2, and NTLM2 + Session authentication out of the box. One can still continue using an external NTLM engine such as JCIFS library developed by the Samba - project as a part of their Windows interoperability suite of programs. For details - please refer to the NTLM_SUPPORT.txt document included with - HttpClient distributions. + project as a part of their Windows interoperability suite of programs.
NTLM connection persistence @@ -432,18 +429,14 @@ HttpContext localContext = new BasicHttpContext(); HttpGet httpget = new HttpGet("/ntlm-protected/info"); HttpResponse response1 = httpclient.execute(target, httpget, localContext); HttpEntity entity1 = response1.getEntity(); -if (entity1 != null) { - entity1.consumeContent(); -} +EntityUtils.consume(entity1); // Execute an expensive method next reusing the same context (and connection) HttpPost httppost = new HttpPost("/ntlm-protected/form"); httppost.setEntity(new StringEntity("lots and lots of data")); HttpResponse response2 = httpclient.execute(target, httppost, localContext); HttpEntity entity2 = response2.getEntity(); -if (entity2 != null) { - entity2.consumeContent(); -} +EntityUtils.consume(entity2); ]]>
@@ -590,11 +583,8 @@ Value: 0x01
- Activating and customizing <literal>SPNEGO</literal> authentication - scheme - Please note SPNEGO authentication scheme is NOT active per - default! - In order to activate SPNEGO support an instance of + Customizing <literal>SPNEGO</literal> authentication scheme + In order to customize SPNEGO support a new instance of NegotiateSchemeFactory class must be created and registered with the authentication scheme registry of HttpClient.
-
- Cache Implementation - - The provided implementation for an HttpCache<T> is called - BasicHttpCache. It uses an in-memory LinkedHashMap to provide basic - least-recently-used (LRU) functionality. Because we provide the - HttpCache<T> interface, other implementations of caching may be - used, e.g. EHCache, memcached, etc. -
-
Example Usage @@ -132,13 +122,37 @@ here are for example only and not intended to be prescriptive or considered as recommendations. - -HttpClient client = new DefaultHttpClient(); -int maxCacheEntries = 1000; -int maxCacheEntrySizeBytes = 8192; -HttpCache<CacheEntry> cache = new -BasicHttpCache(maxCacheEntries); -HttpClient cachingClient = new CachingHttpClient(client, cache, maxCacheEntrySizeBytes); - + +
diff --git a/src/docbkx/connmgmt.xml b/src/docbkx/connmgmt.xml index 94609d20d..1a4271cde 100644 --- a/src/docbkx/connmgmt.xml +++ b/src/docbkx/connmgmt.xml @@ -595,9 +595,7 @@ HttpGet httpget = new HttpGet("http://www.google.com/"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println(response.getStatusLine()); -if (entity != null) { - entity.consumeContent(); -} +EntityUtils.consume(entity); httpclient.getConnectionManager().shutdown(); ]]> @@ -668,10 +666,9 @@ static class GetThread extends Thread { HttpEntity entity = response.getEntity(); if (entity != null) { // do something useful with the entity - // ... - // ensure the connection gets released to the manager - entity.consumeContent(); } + // ensure the connection gets released to the manager + EntityUtils.consume(entity); } catch (Exception ex) { this.httpget.abort(); } diff --git a/src/docbkx/fundamentals.xml b/src/docbkx/fundamentals.xml index 01c5239ed..d6b6c7095 100644 --- a/src/docbkx/fundamentals.xml +++ b/src/docbkx/fundamentals.xml @@ -576,8 +576,7 @@ HttpHost target = (HttpHost) localContext.getAttribute( System.out.println("Final target: " + target); HttpEntity entity = response.getEntity(); -if (entity != null) { -entity.consumeContent(); +EntityUtils.consume(entity); } ]]> stdout > @@ -765,9 +764,7 @@ for (int i = 0; i < 10; i++) { HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); - if (entity != null) { - entity.consumeContent(); - } + EntityUtils.consume(entity); } ]]>