mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-17 15:35:20 +00:00
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
This commit is contained in:
parent
31590950b5
commit
b2ad050679
@ -181,9 +181,7 @@ HttpContext localContext = new BasicHttpContext();
|
|||||||
HttpGet httpget = new HttpGet("http://localhost:8080/");
|
HttpGet httpget = new HttpGet("http://localhost:8080/");
|
||||||
HttpResponse response = httpclient.execute(httpget, localContext);
|
HttpResponse response = httpclient.execute(httpget, localContext);
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
Object userToken = localContext.getAttribute(ClientContext.USER_TOKEN);
|
Object userToken = localContext.getAttribute(ClientContext.USER_TOKEN);
|
||||||
System.out.println(userToken);
|
System.out.println(userToken);
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
@ -200,9 +198,7 @@ HttpContext localContext1 = new BasicHttpContext();
|
|||||||
HttpGet httpget1 = new HttpGet("http://localhost:8080/");
|
HttpGet httpget1 = new HttpGet("http://localhost:8080/");
|
||||||
HttpResponse response1 = httpclient.execute(httpget1, localContext1);
|
HttpResponse response1 = httpclient.execute(httpget1, localContext1);
|
||||||
HttpEntity entity1 = response1.getEntity();
|
HttpEntity entity1 = response1.getEntity();
|
||||||
if (entity1 != null) {
|
EntityUtils.consume(entity1);
|
||||||
entity1.consumeContent();
|
|
||||||
}
|
|
||||||
Principal principal = (Principal) localContext1.getAttribute(
|
Principal principal = (Principal) localContext1.getAttribute(
|
||||||
ClientContext.USER_TOKEN);
|
ClientContext.USER_TOKEN);
|
||||||
|
|
||||||
@ -211,9 +207,7 @@ localContext2.setAttribute(ClientContext.USER_TOKEN, principal);
|
|||||||
HttpGet httpget2 = new HttpGet("http://localhost:8080/");
|
HttpGet httpget2 = new HttpGet("http://localhost:8080/");
|
||||||
HttpResponse response2 = httpclient.execute(httpget2, localContext2);
|
HttpResponse response2 = httpclient.execute(httpget2, localContext2);
|
||||||
HttpEntity entity2 = response2.getEntity();
|
HttpEntity entity2 = response2.getEntity();
|
||||||
if (entity2 != null) {
|
EntityUtils.consume(entity2);
|
||||||
entity2.consumeContent();
|
|
||||||
}
|
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
@ -24,9 +24,8 @@
|
|||||||
<chapter id="authentication">
|
<chapter id="authentication">
|
||||||
<title>HTTP authentication</title>
|
<title>HTTP authentication</title>
|
||||||
<para>HttpClient provides full support for authentication schemes defined by the HTTP standard
|
<para>HttpClient provides full support for authentication schemes defined by the HTTP standard
|
||||||
specification. HttpClient's authentication framework can also be extended to support
|
specification as well as a number of widely used non-standard authentication schemes such
|
||||||
non-standard authentication schemes such as <literal>NTLM</literal> and
|
as <literal>NTLM</literal> and <literal>SPNEGO</literal>.</para>
|
||||||
<literal>SPNEGO</literal>.</para>
|
|
||||||
<section>
|
<section>
|
||||||
<title>User credentials</title>
|
<title>User credentials</title>
|
||||||
<para>Any process of user authentication requires a set of credentials that can be used to
|
<para>Any process of user authentication requires a set of credentials that can be used to
|
||||||
@ -109,9 +108,7 @@ pwd
|
|||||||
<title>NTLM:</title>
|
<title>NTLM:</title>
|
||||||
<para>NTLM is a proprietary authentication scheme developed by Microsoft and
|
<para>NTLM is a proprietary authentication scheme developed by Microsoft and
|
||||||
optimized for Windows platforms. NTLM is believed to be more secure than
|
optimized for Windows platforms. NTLM is believed to be more secure than
|
||||||
Digest. This scheme is requires an external NTLM engine to be functional.
|
Digest.</para>
|
||||||
For details please refer to the <literal>NTLM_SUPPORT.txt</literal> document
|
|
||||||
included with HttpClient distributions.</para>
|
|
||||||
</formalpara>
|
</formalpara>
|
||||||
</listitem>
|
</listitem>
|
||||||
<listitem>
|
<listitem>
|
||||||
@ -194,23 +191,28 @@ httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
|
|||||||
<listitem>
|
<listitem>
|
||||||
<formalpara>
|
<formalpara>
|
||||||
<title>Basic:</title>
|
<title>Basic:</title>
|
||||||
<para>Basic authentication scheme</para>
|
<para>Basic authentication</para>
|
||||||
</formalpara>
|
</formalpara>
|
||||||
</listitem>
|
</listitem>
|
||||||
<listitem>
|
<listitem>
|
||||||
<formalpara>
|
<formalpara>
|
||||||
<title>Digest:</title>
|
<title>Digest:</title>
|
||||||
<para>Digest authentication scheme</para>
|
<para>Digest authentication</para>
|
||||||
|
</formalpara>
|
||||||
|
</listitem>
|
||||||
|
<listitem>
|
||||||
|
<formalpara>
|
||||||
|
<title>NTLM:</title>
|
||||||
|
<para>NTLMv1, NTLMv2, and NTLM2 Session authentication</para>
|
||||||
|
</formalpara>
|
||||||
|
</listitem>
|
||||||
|
<listitem>
|
||||||
|
<formalpara>
|
||||||
|
<title>SPNEGO:</title>
|
||||||
|
<para>SPNEGO/Kerberos authentication</para>
|
||||||
</formalpara>
|
</formalpara>
|
||||||
</listitem>
|
</listitem>
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
<para>Please note <literal>NTLM</literal> and <literal>SPNEGO</literal> schemes are
|
|
||||||
<emphasis>NOT</emphasis> registered per default. The <literal>NTLM</literal> cannot
|
|
||||||
be enabled per default due to licensing and legal reasons. For details on how to
|
|
||||||
enable <literal>NTLM</literal> support please see <link linkend="ntlm">this</link>
|
|
||||||
section. <literal>SPNEGO</literal> setup tends to be system specific and must be
|
|
||||||
properly configured in order to be functional. See <link linkend="spnego">this </link>
|
|
||||||
section for details. </para>
|
|
||||||
</section>
|
</section>
|
||||||
<section>
|
<section>
|
||||||
<title>Credentials provider</title>
|
<title>Credentials provider</title>
|
||||||
@ -376,23 +378,18 @@ HttpGet httpget = new HttpGet("/");
|
|||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section id="ntlm">
|
<section id="ntlm">
|
||||||
<title>NTLM Authentication</title>
|
<title>NTLM Authentication</title>
|
||||||
<para>Currently HttpClient does not provide support for the NTLM authentication scheme out
|
<para>As of version 4.1 HttpClient provides full support for NTLMv1, NTLMv2, and NTLM2
|
||||||
of the box and probably never will. The reasons for that are legal rather than
|
Session authentication out of the box. One can still continue using an external
|
||||||
technical. However, NTLM authentication can be enabled by using an external
|
|
||||||
<literal>NTLM</literal> engine such as <ulink url="http://jcifs.samba.org/">JCIFS
|
<literal>NTLM</literal> engine such as <ulink url="http://jcifs.samba.org/">JCIFS
|
||||||
</ulink> library developed by the <ulink url="http://www.samba.org/">Samba</ulink>
|
</ulink> library developed by the <ulink url="http://www.samba.org/">Samba</ulink>
|
||||||
project as a part of their Windows interoperability suite of programs. For details
|
project as a part of their Windows interoperability suite of programs.
|
||||||
please refer to the <literal>NTLM_SUPPORT.txt</literal> document included with
|
|
||||||
HttpClient distributions.
|
|
||||||
</para>
|
</para>
|
||||||
<section>
|
<section>
|
||||||
<title>NTLM connection persistence</title>
|
<title>NTLM connection persistence</title>
|
||||||
@ -432,18 +429,14 @@ HttpContext localContext = new BasicHttpContext();
|
|||||||
HttpGet httpget = new HttpGet("/ntlm-protected/info");
|
HttpGet httpget = new HttpGet("/ntlm-protected/info");
|
||||||
HttpResponse response1 = httpclient.execute(target, httpget, localContext);
|
HttpResponse response1 = httpclient.execute(target, httpget, localContext);
|
||||||
HttpEntity entity1 = response1.getEntity();
|
HttpEntity entity1 = response1.getEntity();
|
||||||
if (entity1 != null) {
|
EntityUtils.consume(entity1);
|
||||||
entity1.consumeContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute an expensive method next reusing the same context (and connection)
|
// Execute an expensive method next reusing the same context (and connection)
|
||||||
HttpPost httppost = new HttpPost("/ntlm-protected/form");
|
HttpPost httppost = new HttpPost("/ntlm-protected/form");
|
||||||
httppost.setEntity(new StringEntity("lots and lots of data"));
|
httppost.setEntity(new StringEntity("lots and lots of data"));
|
||||||
HttpResponse response2 = httpclient.execute(target, httppost, localContext);
|
HttpResponse response2 = httpclient.execute(target, httppost, localContext);
|
||||||
HttpEntity entity2 = response2.getEntity();
|
HttpEntity entity2 = response2.getEntity();
|
||||||
if (entity2 != null) {
|
EntityUtils.consume(entity2);
|
||||||
entity2.consumeContent();
|
|
||||||
}
|
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
@ -590,11 +583,8 @@ Value: 0x01
|
|||||||
</programlisting>
|
</programlisting>
|
||||||
</section>
|
</section>
|
||||||
<section>
|
<section>
|
||||||
<title>Activating and customizing <literal>SPNEGO</literal> authentication
|
<title>Customizing <literal>SPNEGO</literal> authentication scheme</title>
|
||||||
scheme</title>
|
<para>In order to customize <literal>SPNEGO</literal> support a new instance of
|
||||||
<para>Please note <literal>SPNEGO</literal> authentication scheme is NOT active per
|
|
||||||
default! </para>
|
|
||||||
<para>In order to activate <literal>SPNEGO</literal> support an instance of
|
|
||||||
<classname>NegotiateSchemeFactory</classname> class must be created and
|
<classname>NegotiateSchemeFactory</classname> class must be created and
|
||||||
registered with the authentication scheme registry of HttpClient. </para>
|
registered with the authentication scheme registry of HttpClient. </para>
|
||||||
<programlisting><![CDATA[
|
<programlisting><![CDATA[
|
||||||
|
@ -113,16 +113,6 @@
|
|||||||
requirements.</para>
|
requirements.</para>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section>
|
|
||||||
<title>Cache Implementation</title>
|
|
||||||
|
|
||||||
<para>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.</para>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<title>Example Usage</title>
|
<title>Example Usage</title>
|
||||||
|
|
||||||
@ -132,13 +122,37 @@
|
|||||||
here are for example only and not intended to be prescriptive or
|
here are for example only and not intended to be prescriptive or
|
||||||
considered as recommendations.</para>
|
considered as recommendations.</para>
|
||||||
|
|
||||||
<programlisting>
|
<programlisting><![CDATA[
|
||||||
HttpClient client = new DefaultHttpClient();
|
CacheConfig cacheConfig = new CacheConfig();
|
||||||
int maxCacheEntries = 1000;
|
cacheConfig.setMaxCacheEntries(1000);
|
||||||
int maxCacheEntrySizeBytes = 8192;
|
cacheConfig.setMaxObjectSizeBytes(8192);
|
||||||
HttpCache<CacheEntry> cache = new
|
|
||||||
BasicHttpCache(maxCacheEntries);
|
HttpClient cachingClient = new CachingHttpClient(new DefaultHttpClient(), cacheConfig);
|
||||||
HttpClient cachingClient = new CachingHttpClient(client, cache, maxCacheEntrySizeBytes);
|
|
||||||
</programlisting>
|
HttpContext localContext = new BasicHttpContext();
|
||||||
|
HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");
|
||||||
|
HttpResponse response = cachingClient.execute(httpget, localContext);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute(
|
||||||
|
CachingHttpClient.CACHE_RESPONSE_STATUS);
|
||||||
|
switch (responseStatus) {
|
||||||
|
case CACHE_HIT:
|
||||||
|
System.out.println("A response was generated from the cache with no requests " +
|
||||||
|
"sent upstream");
|
||||||
|
break;
|
||||||
|
case CACHE_MODULE_RESPONSE:
|
||||||
|
System.out.println("The response was generated directly by the caching module");
|
||||||
|
break;
|
||||||
|
case CACHE_MISS:
|
||||||
|
System.out.println("The response came from an upstream server");
|
||||||
|
break;
|
||||||
|
case VALIDATED:
|
||||||
|
System.out.println("The response was generated from the cache after validating " +
|
||||||
|
"the entry with the origin server");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
]]>
|
||||||
|
</programlisting>
|
||||||
</section>
|
</section>
|
||||||
</chapter>
|
</chapter>
|
||||||
|
@ -595,9 +595,7 @@ HttpGet httpget = new HttpGet("http://www.google.com/");
|
|||||||
HttpResponse response = httpclient.execute(httpget);
|
HttpResponse response = httpclient.execute(httpget);
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
System.out.println(response.getStatusLine());
|
System.out.println(response.getStatusLine());
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
httpclient.getConnectionManager().shutdown();
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
</section>
|
</section>
|
||||||
@ -668,10 +666,9 @@ static class GetThread extends Thread {
|
|||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
// do something useful with the entity
|
// 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) {
|
} catch (Exception ex) {
|
||||||
this.httpget.abort();
|
this.httpget.abort();
|
||||||
}
|
}
|
||||||
|
@ -576,8 +576,7 @@ HttpHost target = (HttpHost) localContext.getAttribute(
|
|||||||
System.out.println("Final target: " + target);
|
System.out.println("Final target: " + target);
|
||||||
|
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
}
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
<para>stdout ></para>
|
<para>stdout ></para>
|
||||||
@ -765,9 +764,7 @@ for (int i = 0; i < 10; i++) {
|
|||||||
HttpResponse response = httpclient.execute(httpget, localContext);
|
HttpResponse response = httpclient.execute(httpget, localContext);
|
||||||
|
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
EntityUtils.consume(entity);
|
||||||
entity.consumeContent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]]></programlisting>
|
]]></programlisting>
|
||||||
</section>
|
</section>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user