Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. <html><head>
  2.       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  3.    <title>Chapter&nbsp;1.&nbsp;Fundamentals</title><link rel="stylesheet" href="css/hc-tutorial.css" type="text/css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.73.2"><link rel="start" href="index.html" title="HttpClient Tutorial"><link rel="up" href="index.html" title="HttpClient Tutorial"><link rel="prev" href="preface.html" title="Preface"><link rel="next" href="connmgmt.html" title="Chapter&nbsp;2.&nbsp;Connection management"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div xmlns:fo="http://www.w3.org/1999/XSL/Format" class="banner"><a class="bannerLeft" href="http://www.apache.org/" title="Apache Software Foundation"><img style="border:none;" src="images/asf_logo_wide.gif"></a><a class="bannerRight" href="http://hc.apache.org/httpcomponents-core/" title="Apache HttpComponents Core"><img style="border:none;" src="images/hc_logo.png"></a><div class="clear"></div></div><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&nbsp;1.&nbsp;Fundamentals</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="preface.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="connmgmt.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="fundamentals"></a>Chapter&nbsp;1.&nbsp;Fundamentals</h2></div></div></div>
  4.    
  5.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e37"></a>1.1.&nbsp;Request execution</h2></div></div></div>
  6.        
  7.         <p> The most essential function of HttpClient is to execute HTTP methods. Execution of an
  8.             HTTP method involves one or several HTTP request / HTTP response exchanges, usually
  9.             handled internally by HttpClient. The user is expected to provide a request object to
  10.             execute and HttpClient is expected to transmit the request to the target server return a
  11.             corresponding response object, or throw an exception if execution was unsuccessful. </p>
  12.         <p> Quite naturally, the main entry point of the HttpClient API is the HttpClient
  13.             interface that defines the contract described above. </p>
  14.         <p>Here is an example of request execution process in its simplest form:</p>
  15.         <pre class="programlisting">
  16. HttpClient httpclient = new DefaultHttpClient();
  17. HttpGet httpget = new HttpGet("http://localhost/");
  18. HttpResponse response = httpclient.execute(httpget);
  19. HttpEntity entity = response.getEntity();
  20. if (entity != null) {
  21.     InputStream instream = entity.getContent();
  22.     int l;
  23.     byte[] tmp = new byte[2048];
  24.     while ((l = instream.read(tmp)) != -1) {
  25.     }
  26. }
  27. </pre>
  28.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e43"></a>1.1.1.&nbsp;HTTP request</h3></div></div></div>
  29.            
  30.             <p>All HTTP requests have a request line consisting a method name, a request URI and
  31.                 a HTTP protocol version.</p>
  32.             <p>HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1
  33.                 specification: <code class="literal">GET</code>, <code class="literal">HEAD</code>,
  34.                     <code class="literal">POST</code>, <code class="literal">PUT</code>, <code class="literal">DELETE</code>,
  35.                     <code class="literal">TRACE</code> and <code class="literal">OPTIONS</code>. There is a special
  36.                 class for each method type.: <code class="classname">HttpGet</code>,
  37.                     <code class="classname">HttpHead</code>, <code class="classname">HttpPost</code>,
  38.                     <code class="classname">HttpPut</code>, <code class="classname">HttpDelete</code>,
  39.                     <code class="classname">HttpTrace</code>, and <code class="classname">HttpOptions</code>.</p>
  40.             <p>The Request-URI is a Uniform Resource Identifier that identifies the resource upon
  41.                 which to apply the request. HTTP request URIs consist of a protocol scheme, host
  42.                 name, optional port, resource path, optional query, and optional fragment.</p>
  43.             <pre class="programlisting">
  44. HttpGet httpget = new HttpGet(
  45.      "http://www.google.com/search?hl=en&amp;q=httpclient&amp;btnG=Google+Search&amp;aq=f&amp;oq=");
  46. </pre>
  47.             <p>HttpClient provides a number of utility methods to simplify creation and
  48.                 modification of request URIs.</p>
  49.             <p>URI can be assembled programmatically:</p>
  50.             <pre class="programlisting">
  51. URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
  52.     "q=httpclient&amp;btnG=Google+Search&amp;aq=f&amp;oq=", null);
  53. HttpGet httpget = new HttpGet(uri);
  54. System.out.println(httpget.getURI());
  55. </pre>
  56.             <p>stdout &gt;</p>
  57.             <pre class="programlisting">
  58. http://www.google.com/search?q=httpclient&amp;btnG=Google+Search&amp;aq=f&amp;oq=
  59. </pre>
  60.             <p>Query string can also be generated from individual parameters:</p>
  61.             <pre class="programlisting">
  62. List&lt;NameValuePair&gt; qparams = new ArrayList&lt;NameValuePair&gt;();
  63. qparams.add(new BasicNameValuePair("q", "httpclient"));
  64. qparams.add(new BasicNameValuePair("btnG", "Google Search"));
  65. qparams.add(new BasicNameValuePair("aq", "f"));
  66. qparams.add(new BasicNameValuePair("oq", null));
  67. URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
  68.     URLEncodedUtils.format(qparams, "UTF-8"), null);
  69. HttpGet httpget = new HttpGet(uri);
  70. System.out.println(httpget.getURI());
  71. </pre>
  72.             <p>stdout &gt;</p>
  73.             <pre class="programlisting">
  74. http://www.google.com/search?q=httpclient&amp;btnG=Google+Search&amp;aq=f&amp;oq=
  75. </pre>
  76.         </div>
  77.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e72"></a>1.1.2.&nbsp;HTTP response</h3></div></div></div>
  78.            
  79.             <p>HTTP response is a message sent by the server back to the client after having
  80.                 received and interpreted a request message. The first line of that message consists
  81.                 of the protocol version followed by a numeric status code and its associated textual
  82.                 phrase.</p>
  83.             <pre class="programlisting">
  84. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  85. HttpStatus.SC_OK, "OK");
  86.  
  87. System.out.println(response.getProtocolVersion());
  88. System.out.println(response.getStatusLine().getStatusCode());
  89. System.out.println(response.getStatusLine().getReasonPhrase());
  90. System.out.println(response.getStatusLine().toString());
  91. </pre>
  92.             <p>stdout &gt;</p>
  93.             <pre class="programlisting">
  94. HTTP/1.1
  95. 200
  96. OK
  97. HTTP/1.1 200 OK
  98. </pre>
  99.         </div>
  100.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e78"></a>1.1.3.&nbsp;Working with message headers</h3></div></div></div>
  101.            
  102.             <p>An HTTP message can contain a number of headers describing properties of the
  103.                 message such as the content length, content type and so on. HttpClient provides
  104.                 methods to retrieve, add, remove and enumerate headers.</p>
  105.             <pre class="programlisting">
  106. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  107.     HttpStatus.SC_OK, "OK");
  108. response.addHeader("Set-Cookie",
  109.     "c1=a; path=/; domain=localhost");
  110. response.addHeader("Set-Cookie",
  111.     "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
  112. Header h1 = response.getFirstHeader("Set-Cookie");
  113. System.out.println(h1);
  114. Header h2 = response.getLastHeader("Set-Cookie");
  115. System.out.println(h2);
  116. Header[] hs = response.getHeaders("Set-Cookie");
  117. System.out.println(hs.length);
  118. </pre>
  119.             <p>stdout &gt;</p>
  120.             <pre class="programlisting">
  121. Set-Cookie: c1=a; path=/; domain=localhost
  122. Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
  123. 2
  124. </pre>
  125.             <p>The most efficient way to obtain all headers of a given type is by using the
  126.                     <code class="interfacename">HeaderIterator</code> interface.</p>
  127.             <pre class="programlisting">
  128. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  129.     HttpStatus.SC_OK, "OK");
  130. response.addHeader("Set-Cookie",
  131.     "c1=a; path=/; domain=localhost");
  132. response.addHeader("Set-Cookie",
  133.     "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
  134.  
  135. HeaderIterator it = response.headerIterator("Set-Cookie");
  136.  
  137. while (it.hasNext()) {
  138.     System.out.println(it.next());
  139. }
  140. </pre>
  141.             <p>stdout &gt;</p>
  142.             <pre class="programlisting">
  143. Set-Cookie: c1=a; path=/; domain=localhost
  144. Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
  145. </pre>
  146.             <p>It also provides convenience methods to parse HTTP messages into individual header
  147.                 elements.</p>
  148.             <pre class="programlisting">
  149. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  150.     HttpStatus.SC_OK, "OK");
  151. response.addHeader("Set-Cookie",
  152.     "c1=a; path=/; domain=localhost");
  153. response.addHeader("Set-Cookie",
  154.     "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
  155.  
  156. HeaderElementIterator it = new BasicHeaderElementIterator(
  157.     response.headerIterator("Set-Cookie"));
  158.  
  159. while (it.hasNext()) {
  160.     HeaderElement elem = it.nextElement();
  161.     System.out.println(elem.getName() + " = " + elem.getValue());
  162.     NameValuePair[] params = elem.getParameters();
  163.     for (int i = 0; i &lt; params.length; i++) {
  164.         System.out.println(" " + params[i]);
  165.     }
  166. }
  167. </pre>
  168.             <p>stdout &gt;</p>
  169.             <pre class="programlisting">
  170. c1 = a
  171. path=/
  172. domain=localhost
  173. c2 = b
  174. path=/
  175. c3 = c
  176. domain=localhost
  177. </pre>
  178.         </div>
  179.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e93"></a>1.1.4.&nbsp;HTTP entity</h3></div></div></div>
  180.            
  181.             <p>HTTP messages can carry a content entity associated with the request or response.
  182.                 Entities can be found in some requests and in some responses, as they are optional.
  183.                 Requests that use entities are referred to as entity enclosing requests. The HTTP
  184.                 specification defines two entity enclosing methods: <code class="literal">POST</code> and
  185.                     <code class="literal">PUT</code>. Responses are usually expected to enclose a content
  186.                 entity. There are exceptions to this rule such as responses to
  187.                     <code class="literal">HEAD</code> method and <code class="literal">204 No Content</code>,
  188.                     <code class="literal">304 Not Modified</code>, <code class="literal">205 Reset Content</code>
  189.                 responses.</p>
  190.             <p>HttpClient distinguishes three kinds of entities, depending on where their content
  191.                 originates:</p>
  192.             <div class="itemizedlist"><ul type="disc"><li>
  193.                     <p>
  194.                         <b>streamed:&nbsp;</b>
  195.                         The content is received from a stream, or generated on the fly. In
  196.                             particular, this category includes entities being received from HTTP
  197.                             responses. Streamed entities are generally not repeatable.
  198.                     </p>
  199.                 </li><li>
  200.                     <p>
  201.                         <b>self-contained:&nbsp;</b>
  202.                         The content is in memory or obtained by means that are independent
  203.                             from a connection or other entity. Self-contained entities are generally
  204.                             repeatable. This type of entities will be mostly used for entity
  205.                             enclosing HTTP requests.
  206.                     </p>
  207.                 </li><li>
  208.                     <p>
  209.                         <b>wrapping:&nbsp;</b>
  210.                         The content is obtained from another entity.
  211.                     </p>
  212.                 </li></ul></div>
  213.             <p>This distinction is important for connection management when streaming out content
  214.                 from an HTTP response. For request entities that are created by an application and
  215.                 only sent using HttpClient, the difference between streamed and self-contained is of
  216.                 little importance. In that case, it is suggested to consider non-repeatable entities
  217.                 as streamed, and those that are repeatable as self-contained.</p>
  218.             <div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="d4e117"></a>1.1.4.1.&nbsp;Repeatable entities</h4></div></div></div>
  219.                
  220.                 <p>An entity can be repeatable, meaning its content can be read more than once.
  221.                     This is only possible with self contained entities (like
  222.                         <code class="classname">ByteArrayEntity</code> or
  223.                         <code class="classname">StringEntity</code>)</p>
  224.             </div>
  225.             <div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="d4e122"></a>1.1.4.2.&nbsp;Using HTTP entities</h4></div></div></div>
  226.                
  227.                 <p>Since an entity can represent both binary and character content, it has
  228.                     support for character encodings (to support the latter, ie. character
  229.                     content).</p>
  230.                 <p>The entity is created when executing a request with enclosed content or when
  231.                     the request was successful and the response body is used to send the result back
  232.                     to the client.</p>
  233.                 <p>To read the content from the entity, one can either retrieve the input stream
  234.                     via the <code class="methodname">HttpEntity#getContent()</code> method, which returns
  235.                     an <code class="classname">java.io.InputStream</code>, or one can supply an output
  236.                     stream to the <code class="methodname">HttpEntity#writeTo(OutputStream)</code> method,
  237.                     which will return once all content has been written to the given stream.</p>
  238.                 <p>When the entity has been received with an incoming message, the methods
  239.                         <code class="methodname">HttpEntity#getContentType()</code> and
  240.                         <code class="methodname">HttpEntity#getContentLength()</code> methods can be used
  241.                     for reading the common metadata such as <code class="literal">Content-Type</code> and
  242.                         <code class="literal">Content-Length</code> headers (if they are available). Since the
  243.                         <code class="literal">Content-Type</code> header can contain a character encoding for
  244.                     text mime-types like text/plain or text/html, the
  245.                         <code class="methodname">HttpEntity#getContentEncoding()</code> method is used to
  246.                     read this information. If the headers aren't available, a length of -1 will be
  247.                    returned, and NULL for the content type. If the <code class="literal">Content-Type</code>
  248.                    header is available, a <code class="interfacename">Header</code> object will be
  249.                    returned.</p>
  250.                <p>When creating an entity for a outgoing message, this meta data has to be
  251.                    supplied by the creator of the entity.</p>
  252.                <pre class="programlisting">
  253. StringEntity myEntity = new StringEntity("important message",
  254.    "UTF-8");
  255.  
  256. System.out.println(myEntity.getContentType());
  257. System.out.println(myEntity.getContentLength());
  258. System.out.println(EntityUtils.getContentCharSet(myEntity));
  259. System.out.println(EntityUtils.toString(myEntity));
  260. System.out.println(EntityUtils.toByteArray(myEntity).length);
  261. </pre>
  262.                <p>stdout &gt;</p>
  263.                <pre class="programlisting">
  264. Content-Type: text/plain; charset=UTF-8
  265. 17
  266. UTF-8
  267. important message
  268. 17
  269. </pre>
  270.            </div>
  271.        </div>
  272.        <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e143"></a>1.1.5.&nbsp;Ensuring release of low level resources</h3></div></div></div>
  273.            
  274.            <p>When finished with a response entity, it's important to ensure that all entity
  275.                 content has been fully consumed, so that the connection could be safely returned to
  276.                 the connection pool and re-used by the connection manager for subsequent requests.
  277.                 The easiest way to do so is to call the
  278.                     <code class="methodname">HttpEntity#consumeContent(</code>) method to consume any
  279.                 available content on the stream. HttpClient will automatically release the
  280.                 underlying connection back to the connection manager as soon as it detects that the
  281.                 end of the content stream has been reached. The
  282.                     <code class="methodname">HttpEntity#consumeContent()</code> method is safe to call more
  283.                 than once.</p>
  284.             <p>There can be situations, however, when only a small portion of the entire response
  285.                 content needs to be retrieved and the performance penalty for consuming the
  286.                 remaining content and making the connection reusable is too high, one can simply
  287.                 terminate the request by calling <code class="methodname">HttpUriRequest#abort()</code>
  288.                 method.</p>
  289.             <pre class="programlisting">
  290. HttpGet httpget = new HttpGet("http://localhost/");
  291. HttpResponse response = httpclient.execute(httpget);
  292. HttpEntity entity = response.getEntity();
  293. if (entity != null) {
  294.     InputStream instream = entity.getContent();
  295.     int byteOne = instream.read();
  296.     int byteTwo = instream.read();
  297.     // Do not need the rest
  298.     httpget.abort();
  299. }
  300. </pre>
  301.             <p>The connection will not be reused, but all level resources held by it will be
  302.                 correctly deallocated.</p>
  303.         </div>
  304.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e152"></a>1.1.6.&nbsp;Consuming entity content</h3></div></div></div>
  305.            
  306.             <p>The recommended way to consume content of an entity is by using its
  307.                     <code class="methodname">HttpEntity#getContent()</code> or
  308.                     <code class="methodname">HttpEntity#writeTo(OutputStream)</code> methods. HttpClient
  309.                 also comes with the <code class="classname">EntityUtils</code> class, which exposes several
  310.                 static methods to more easily read the content or information from an entity.
  311.                 Instead of reading the <code class="classname">java.io.InputStream</code> directly, one can
  312.                 retrieve the whole content body in a string / byte array by using the methods from
  313.                 this class. However, the use of <code class="classname">EntityUtils</code> is
  314.                 strongly discouraged unless the response entities originate from a trusted HTTP
  315.                 server and are known to be of limited length.</p>
  316.             <pre class="programlisting">
  317. HttpGet httpget = new HttpGet("http://localhost/");
  318. HttpResponse response = httpclient.execute(httpget);
  319. HttpEntity entity = response.getEntity();
  320. if (entity != null) {
  321.     long len = entity.getContentLength();
  322.     if (len != -1 &amp;&amp; len &lt; 2048) {
  323.         System.out.println(EntityUtils.toString(entity));
  324.     } else {
  325.         // Stream content out
  326.     }
  327. }
  328. </pre>
  329.             <p>In some situations it may be necessary to be able to read entity content more than
  330.                 once. In this case entity content must be buffered in some way, either in memory or
  331.                 on disk. The simplest way to accomplish that is by wrapping the original entity with
  332.                 the <code class="classname">BufferedHttpEntity</code> class. This will cause the content of
  333.                 the original entity to be read into a in-memory buffer. In all other ways the entity
  334.                 wrapper will be have the original one.</p>
  335.             <pre class="programlisting">
  336. HttpGet httpget = new HttpGet("http://localhost/");
  337. HttpResponse response = httpclient.execute(httpget);
  338. HttpEntity entity = response.getEntity();
  339. if (entity != null) {
  340.     entity = new BufferedHttpEntity(entity);
  341. }
  342. </pre>
  343.         </div>
  344.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e164"></a>1.1.7.&nbsp;Producing entity content</h3></div></div></div>
  345.            
  346.             <p>HttpClient provides several classes that can be used to efficiently stream out
  347.                 content though HTTP connections. Instances of those classes can be associated with
  348.                 entity enclosing requests such as <code class="literal">POST</code> and <code class="literal">PUT</code>
  349.                 in order to enclose entity content into outgoing HTTP requests. HttpClient provides
  350.                 several classes for most common data containers such as string, byte array, input
  351.                 stream, and file: <code class="classname">StringEntity</code>,
  352.                     <code class="classname">ByteArrayEntity</code>,
  353.                 <code class="classname">InputStreamEntity</code>, and
  354.                 <code class="classname">FileEntity</code>.</p>
  355.             <pre class="programlisting">
  356. File file = new File("somefile.txt");
  357. FileEntity entity = new FileEntity(file, "text/plain; charset=\"UTF-8\"");
  358.  
  359. HttpPost httppost = new HttpPost("http://localhost/action.do");
  360. httppost.setEntity(entity);
  361. </pre>
  362.             <p>Please note <code class="classname">InputStreamEntity</code> is not repeatable, because it
  363.                 can only read from the underlying data stream once. Generally it is recommended to
  364.                 implement a custom <code class="interfacename">HttpEntity</code> class which is
  365.                 self-contained instead of using generic <code class="classname">InputStreamEntity</code>.
  366.                     <code class="classname">FileEntity</code> can be a good starting point.</p>
  367.             <div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="d4e179"></a>1.1.7.1.&nbsp;Dynamic content entities</h4></div></div></div>
  368.                
  369.                 <p>Often HTTP entities need to be generated dynamically based a particular
  370.                     execution context. HttpClient provides support for dynamic entities by using
  371.                         <code class="classname">EntityTemplate</code> entity class and
  372.                         <code class="interfacename">ContentProducer</code> interface. Content producers
  373.                     are objects which produce their content on demand, by writing it out to an
  374.                     output stream. They are expected to be able produce their content every time
  375.                     they are requested to do so. So entities created with
  376.                         <code class="classname">EntityTemplate</code> are generally self-contained and
  377.                     repeatable.</p>
  378.                 <pre class="programlisting">
  379. ContentProducer cp = new ContentProducer() {
  380.     public void writeTo(OutputStream outstream) throws IOException {
  381.         Writer writer = new OutputStreamWriter(outstream, "UTF-8");
  382.         writer.write("&lt;response&gt;");
  383.         writer.write("  &lt;content&gt;");
  384.         writer.write("    important stuff");
  385.         writer.write("  &lt;/content&gt;");
  386.         writer.write("&lt;/response&gt;");
  387.         writer.flush();
  388.     }
  389. };
  390. HttpEntity entity = new EntityTemplate(cp);
  391. HttpPost httppost = new HttpPost("http://localhost/handler.do");
  392. httppost.setEntity(entity);
  393. </pre>
  394.             </div>
  395.             <div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="d4e186"></a>1.1.7.2.&nbsp;HTML forms</h4></div></div></div>
  396.                
  397.                 <p>Many applications frequently need to simulate the process of submitting an
  398.                     HTML form, for instance, in order to log in to a web application or submit input
  399.                     data. HttpClient provides special entity class
  400.                         <code class="classname">UrlEncodedFormEntity</code> to facilitate the
  401.                     process.</p>
  402.                 <pre class="programlisting">
  403. List&lt;NameValuePair&gt; formparams = new ArrayList&lt;NameValuePair&gt;();
  404. formparams.add(new BasicNameValuePair("param1", "value1"));
  405. formparams.add(new BasicNameValuePair("param2", "value2"));
  406. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
  407. HttpPost httppost = new HttpPost("http://localhost/handler.do");
  408. httppost.setEntity(entity);
  409. </pre>
  410.                 <p>This <code class="classname">UrlEncodedFormEntity</code> instance will use the so
  411.                     called URL encoding to encode parameters and produce the following
  412.                     content:</p>
  413.                 <pre class="programlisting">
  414. param1=value1&amp;param2=value2
  415. </pre>
  416.             </div>
  417.             <div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="d4e194"></a>1.1.7.3.&nbsp;Content chunking</h4></div></div></div>
  418.                
  419.                 <p>Generally it is recommended to let HttpClient choose the most appropriate
  420.                     transfer encoding based on the properties of the HTTP message being transferred.
  421.                     It is possible, however, to inform HttpClient that the chunk coding is preferred
  422.                     by setting <code class="methodname">HttpEntity#setChunked()</code> to true. Please note
  423.                     that HttpClient will use this flag as a hint only. This value well be ignored
  424.                     when using HTTP protocol versions that do not support chunk coding, such as
  425.                     HTTP/1.0.</p>
  426.                 <pre class="programlisting">
  427. StringEntity entity = new StringEntity("important message",
  428.     "text/plain; charset=\"UTF-8\"");
  429. entity.setChunked(true);
  430. HttpPost httppost = new HttpPost("http://localhost/acrtion.do");
  431. httppost.setEntity(entity);
  432. </pre>
  433.             </div>
  434.         </div>
  435.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e199"></a>1.1.8.&nbsp;Response handlers</h3></div></div></div>
  436.            
  437.             <p>The simplest and the most convenient way to handle responses is by using
  438.                     <code class="interfacename">ResponseHandler</code> interface. This method completely
  439.                 relieves the user from having to worry about connection management. When using a
  440.                     <code class="interfacename">ResponseHandler</code> HttpClient will automatically
  441.                 take care of ensuring release of the connection back to the connection manager
  442.                 regardless whether the request execution succeeds or causes an exception.</p>
  443.             <pre class="programlisting">
  444. HttpClient httpclient = new DefaultHttpClient();
  445. HttpGet httpget = new HttpGet("http://localhost/");
  446.  
  447. ResponseHandler&lt;byte[]&gt; handler = new ResponseHandler&lt;byte[]&gt;() {
  448.     public byte[] handleResponse(
  449.             HttpResponse response) throws ClientProtocolException, IOException {
  450.         HttpEntity entity = response.getEntity();
  451.         if (entity != null) {
  452.             return EntityUtils.toByteArray(entity);
  453.         } else {
  454.             return null;
  455.         }
  456.     }
  457. };
  458.  
  459. byte[] response = httpclient.execute(httpget, handler);
  460. </pre>
  461.         </div>
  462.     </div>
  463.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e205"></a>1.2.&nbsp;HTTP execution context</h2></div></div></div>
  464.        
  465.         <p>Originally HTTP has been designed as a stateless, response-request oriented protocol.
  466.             However, real world applications often need to be able to persist state information
  467.             through several logically related request-response exchanges. In order to enable
  468.             applications to maintain a processing state HttpClient allows HTTP requests to be
  469.             executed within a particular execution context, referred to as HTTP context. Multiple
  470.             logically related requests can participate in a logical session if the same context is
  471.             reused between consecutive requests. HTTP context functions similarly to
  472.                 <code class="interfacename">java.util.Map&lt;String, Object&gt;</code>. It is
  473.             simply a collection of arbitrary named values. Application can populate context
  474.             attributes prior to a request execution or examine the context after the execution has
  475.             been completed.</p>
  476.         <p>In the course of HTTP request execution HttpClient adds the following attributes to
  477.             the execution context:</p>
  478.         <div class="itemizedlist"><ul type="disc"><li>
  479.                 <p>
  480.                     <b>'http.connection':&nbsp;</b>
  481.                     <code class="interfacename">HttpConnection</code> instance representing the
  482.                         actual connection to the target server.
  483.                 </p>
  484.             </li><li>
  485.                 <p>
  486.                     <b>'http.target_host':&nbsp;</b>
  487.                     <code class="classname">HttpHost</code> instance representing the connection
  488.                         target.
  489.                 </p>
  490.             </li><li>
  491.                 <p>
  492.                     <b>'http.proxy_host':&nbsp;</b>
  493.                     <code class="classname">HttpHost</code> instance representing the connection
  494.                         proxy, if used
  495.                 </p>
  496.             </li><li>
  497.                 <p>
  498.                     <b>'http.request':&nbsp;</b>
  499.                     <code class="interfacename">HttpRequest</code> instance representing the
  500.                         actual HTTP request.
  501.                 </p>
  502.             </li><li>
  503.                 <p>
  504.                     <b>'http.response':&nbsp;</b>
  505.                     <code class="interfacename">HttpResponse</code> instance representing the
  506.                         actual HTTP response.
  507.                 </p>
  508.             </li><li>
  509.                 <p>
  510.                     <b>'http.request_sent':&nbsp;</b>
  511.                     <code class="classname">java.lang.Boolean</code> object representing the flag
  512.                         indicating whether the actual request has been fully transmitted to the
  513.                         connection target.
  514.                 </p>
  515.             </li></ul></div>
  516.         <p>For instance, in order to determine the final redirect target, one can examine the
  517.             value of the <code class="literal">http.target_host</code> attribute after the request
  518.             execution:</p>
  519.         <pre class="programlisting">
  520. DefaultHttpClient httpclient = new DefaultHttpClient();
  521.  
  522. HttpContext localContext = new BasicHttpContext();
  523. HttpGet httpget = new HttpGet("http://www.google.com/");
  524.  
  525. HttpResponse response = httpclient.execute(httpget, localContext);
  526.  
  527. HttpHost target = (HttpHost) localContext.getAttribute(
  528.     ExecutionContext.HTTP_TARGET_HOST);
  529.  
  530. System.out.println("Final target: " + target);
  531.  
  532. HttpEntity entity = response.getEntity();
  533. if (entity != null) {
  534. entity.consumeContent();
  535. }
  536. </pre>
  537.         <p>stdout &gt;</p>
  538.         <pre class="programlisting">
  539. Final target: http://www.google.ch
  540. </pre>
  541.     </div>
  542.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e246"></a>1.3.&nbsp;Exception handling</h2></div></div></div>
  543.        
  544.         <p>HttpClient can throw two types of exceptions:
  545.                 <code class="exceptionname">java.io.IOException</code> in case of an I/O failure such as
  546.             socket timeout or an socket reset and <code class="exceptionname">HttpException</code> that
  547.             signals an HTTP failure such as a violation of the HTTP protocol. Usually I/O errors are
  548.             considered non-fatal and recoverable, whereas HTTP protocol errors are considered fatal
  549.             and cannot be automatically recovered from.</p>
  550.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e251"></a>1.3.1.&nbsp;HTTP transport safety</h3></div></div></div>
  551.            
  552.             <p>It is important to understand that the HTTP protocol is not well suited for all
  553.                 types of applications. HTTP is a simple request/response oriented protocol which was
  554.                 initially designed to support static or dynamically generated content retrieval. It
  555.                 has never been intended to support transactional operations. For instance, the HTTP
  556.                 server will consider its part of the contract fulfilled if it succeeds in receiving
  557.                 and processing the request, generating a response and sending a status code back to
  558.                 the client. The server will make no attempts to roll back the transaction if the
  559.                 client fails to receive the response in its entirety due to a read timeout, a
  560.                 request cancellation or a system crash. If the client decides to retry the same
  561.                 request, the server will inevitably end up executing the same transaction more than
  562.                 once. In some cases this may lead to application data corruption or inconsistent
  563.                 application state.</p>
  564.             <p>Even though HTTP has never been designed to support transactional processing, it
  565.                 can still be used as a transport protocol for mission critical applications provided
  566.                 certain conditions are met. To ensure HTTP transport layer safety the system must
  567.                 ensure the idempotency of HTTP methods on the application layer.</p>
  568.         </div>
  569.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e255"></a>1.3.2.&nbsp;Idempotent methods</h3></div></div></div>
  570.            
  571.             <p>HTTP/1.1 specification defines idempotent method as</p>
  572.             <p>
  573.                 [<span class="citation">Methods can also have the property of "idempotence" in
  574.                     that (aside from error or expiration issues) the side-effects of N &gt; 0
  575.                     identical requests is the same as for a single request</span>]
  576.             </p>
  577.             <p>In other words the application ought to ensure that it is prepared to deal with
  578.                 the implications of multiple execution of the same method. This can be achieved, for
  579.                 instance, by providing a unique transaction id and by other means of avoiding
  580.                 execution of the same logical operation.</p>
  581.             <p>Please note that this problem is not specific to HttpClient. Browser based
  582.                 applications are subject to exactly the same issues related to HTTP methods
  583.                 non-idempotency.</p>
  584.             <p>HttpClient assumes non-entity enclosing methods such as <code class="literal">GET</code> and
  585.                     <code class="literal">HEAD</code> to be idempotent and entity enclosing methods such as
  586.                     <code class="literal">POST</code> and <code class="literal">PUT</code> to be not.</p>
  587.         </div>
  588.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e267"></a>1.3.3.&nbsp;Automatic exception recovery</h3></div></div></div>
  589.            
  590.             <p>By default HttpClient attempts to automatically recover from I/O exceptions. The
  591.                 default auto-recovery mechanism is limited to just a few exceptions that are known
  592.                 to be safe.</p>
  593.             <div class="itemizedlist"><ul type="disc"><li>
  594.                     <p>HttpClient will make no attempt to recover from any logical or HTTP
  595.                         protocol errors (those derived from
  596.                             <code class="exceptionname">HttpException</code> class).</p>
  597.                 </li><li>
  598.                     <p>HttpClient will automatically retry those methods that are assumed to be
  599.                         idempotent.</p>
  600.                 </li><li>
  601.                     <p>HttpClient will automatically retry those methods that fail with a
  602.                         transport exception while the HTTP request is still being transmitted to the
  603.                         target server (i.e. the request has not been fully transmitted to the
  604.                         server).</p>
  605.                 </li><li>
  606.                     <p>HttpClient will automatically retry those methods that have been fully
  607.                         transmitted to the server, but the server failed to respond with an HTTP
  608.                         status code (the server simply drops the connection without sending anything
  609.                         back). In this case it is assumed that the request has not been processed by
  610.                         the server and the application state has not changed. If this assumption may
  611.                         not hold true for the web server your application is targeting it is highly
  612.                         recommended to provide a custom exception handler.</p>
  613.                 </li></ul></div>
  614.         </div>
  615.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e280"></a>1.3.4.&nbsp;Request retry handler</h3></div></div></div>
  616.            
  617.             <p>In order to enable a custom exception recovery mechanism one should provide an
  618.                 implementation of the <code class="interfacename">HttpRequestRetryHandler</code>
  619.                 interface.</p>
  620.             <pre class="programlisting">
  621. DefaultHttpClient httpclient = new DefaultHttpClient();
  622.  
  623. HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
  624.  
  625.     public boolean retryRequest(
  626.             IOException exception,
  627.             int executionCount,
  628.             HttpContext context) {
  629.         if (executionCount &gt;= 5) {
  630.             // Do not retry if over max retry count
  631.             return false;
  632.         }
  633.         if (exception instanceof NoHttpResponseException) {
  634.             // Retry if the server dropped connection on us
  635.             return true;
  636.         }
  637.         if (exception instanceof SSLHandshakeException) {
  638.             // Do not retry on SSL handshake exception
  639.             return false;
  640.         }
  641.         HttpRequest request = (HttpRequest) context.getAttribute(
  642.                 ExecutionContext.HTTP_REQUEST);
  643.         boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
  644.         if (idempotent) {
  645.             // Retry if the request is considered idempotent
  646.             return true;
  647.         }
  648.         return false;
  649.     }
  650.  
  651. };
  652.  
  653. httpclient.setHttpRequestRetryHandler(myRetryHandler);
  654. </pre>
  655.         </div>
  656.     </div>
  657.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e285"></a>1.4.&nbsp;Aborting requests</h2></div></div></div>
  658.        
  659.         <p>In some situations HTTP request execution fail to complete within the expected time
  660.             frame due to high load on the target server or too many concurrent requests issued on
  661.             the client side. In such cases it may be necessary to terminate the request prematurely
  662.             and unblock the execution thread blocked in a I/O operation. HTTP requests being
  663.             executed by HttpClient can be aborted at any stage of execution by invoking
  664.                 <code class="methodname">HttpUriRequest#abort()</code> method. This method is thread-safe
  665.             and can be called from any thread. When an HTTP request is aborted its execution thread
  666.             blocked in an I/O operation is guaranteed to unblock by throwing a
  667.                 <code class="exceptionname">InterruptedIOException</code></p>
  668.     </div>
  669.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e290"></a>1.5.&nbsp;HTTP protocol interceptors</h2></div></div></div>
  670.        
  671.         <p>HTTP protocol interceptor is a routine that implements a specific aspect of the HTTP
  672.             protocol. Usually protocol interceptors are expected to act upon one specific header or
  673.             a group of related headers of the incoming message or populate the outgoing message with
  674.             one specific header or a group of related headers. Protocol interceptors can also
  675.             manipulate content entities enclosed with messages, transparent content compression /
  676.             decompression being a good example. Usually this is accomplished by using the
  677.             'Decorator' pattern where a wrapper entity class is used to decorate the original
  678.             entity. Several protocol interceptors can be combined to form one logical unit.</p>
  679.         <p>Protocol interceptors can collaborate by sharing information - such as a processing
  680.             state - through the HTTP execution context. Protocol interceptors can use HTTP context
  681.             to store a processing state for one request or several consecutive requests.</p>
  682.         <p>Usually the order in which interceptors are executed should not matter as long as they
  683.             do not depend on a particular state of the execution context. If protocol interceptors
  684.             have interdependencies and therefore must be executed in a particular order, they should
  685.             be added to the protocol processor in the same sequence as their expected execution
  686.             order.</p>
  687.         <p>Protocol interceptors must be implemented as thread-safe. Similarly to servlets,
  688.             protocol interceptors should not use instance variables unless access to those variables
  689.             is synchronized.</p>
  690.         <p>This is an example of how local context can be used to persist a processing state
  691.             between consecutive requests:</p>
  692.         <pre class="programlisting">
  693. DefaultHttpClient httpclient = new DefaultHttpClient();
  694.  
  695. HttpContext localContext = new BasicHttpContext();
  696.  
  697. AtomicInteger count = new AtomicInteger(1);
  698.  
  699. localContext.setAttribute("count", count);
  700.  
  701. httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
  702.  
  703.     public void process(
  704.             final HttpRequest request,
  705.             final HttpContext context) throws HttpException, IOException {
  706.         AtomicInteger count = (AtomicInteger) context.getAttribute("count");
  707.         request.addHeader("Count", Integer.toString(count.getAndIncrement()));
  708.     }
  709.    
  710. });
  711.  
  712. HttpGet httpget = new HttpGet("http://localhost/");
  713. for (int i = 0; i &lt; 10; i++) {
  714.     HttpResponse response = httpclient.execute(httpget, localContext);
  715.    
  716.     HttpEntity entity = response.getEntity();
  717.     if (entity != null) {
  718.         entity.consumeContent();
  719.     }
  720. }
  721. </pre>
  722.     </div>
  723.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e298"></a>1.6.&nbsp;HTTP parameters</h2></div></div></div>
  724.        
  725.         <p>HttpParams interface represents a collection of immutable values that define a runtime
  726.             behavior of a component. In many ways <code class="interfacename">HttpParams</code> is
  727.             similar to <code class="interfacename">HttpContext</code>. The main distinction between the
  728.             two lies in their use at runtime. Both interfaces represent a collection of objects that
  729.             are organized as a map of keys to object values, but serve distinct purposes:</p>
  730.         <div class="itemizedlist"><ul type="disc"><li>
  731.                 <p><code class="interfacename">HttpParams</code> is intended to contain simple
  732.                     objects: integers, doubles, strings, collections and objects that remain
  733.                     immutable at runtime.</p>
  734.             </li><li>
  735.                 <p>
  736.                     <code class="interfacename">HttpParams</code> is expected to be used in the 'write
  737.                    once - ready many' mode. <code class="interfacename">HttpContext</code> is intended
  738.                     to contain complex objects that are very likely to mutate in the course of HTTP
  739.                     message processing. </p>
  740.             </li><li>
  741.                 <p>The purpose of <code class="interfacename">HttpParams</code> is to define a
  742.                     behavior of other components. Usually each complex component has its own
  743.                         <code class="interfacename">HttpParams</code> object. The purpose of
  744.                         <code class="interfacename">HttpContext</code> is to represent an execution
  745.                     state of an HTTP process. Usually the same execution context is shared among
  746.                     many collaborating objects.</p>
  747.             </li></ul></div>
  748.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e316"></a>1.6.1.&nbsp;Parameter hierarchies</h3></div></div></div>
  749.            
  750.             <p>In the course of HTTP request execution <code class="interfacename">HttpParams</code>
  751.                 of the <code class="interfacename">HttpRequest</code> object are linked together with
  752.                     <code class="interfacename">HttpParams</code> of the
  753.                     <code class="interfacename">HttpClient</code> instance used to execute the request.
  754.                 This enables parameters set at the HTTP request level take precedence over
  755.                     <code class="interfacename">HttpParams</code> set at the HTTP client level. The
  756.                 recommended practice is to set common parameters shared by all HTTP requests at the
  757.                 HTTP client level and selectively override specific parameters at the HTTP request
  758.                 level.</p>
  759.             <pre class="programlisting">
  760. DefaultHttpClient httpclient = new DefaultHttpClient();
  761. httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
  762.     HttpVersion.HTTP_1_0);
  763. httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,
  764.     "UTF-8");
  765.  
  766. HttpGet httpget = new HttpGet("http://www.google.com/");
  767. httpget.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
  768.     HttpVersion.HTTP_1_1);
  769. httpget.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
  770.     Boolean.FALSE);
  771.  
  772. httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
  773.  
  774.     public void process(
  775.             final HttpRequest request,
  776.             final HttpContext context) throws HttpException, IOException {
  777.         System.out.println(request.getParams().getParameter(
  778.                 CoreProtocolPNames.PROTOCOL_VERSION));
  779.         System.out.println(request.getParams().getParameter(
  780.                 CoreProtocolPNames.HTTP_CONTENT_CHARSET));
  781.         System.out.println(request.getParams().getParameter(
  782.                 CoreProtocolPNames.USE_EXPECT_CONTINUE));
  783.         System.out.println(request.getParams().getParameter(
  784.                 CoreProtocolPNames.STRICT_TRANSFER_ENCODING));
  785.     }
  786.    
  787. });
  788. </pre>
  789.             <p>stdout &gt;</p>
  790.             <pre class="programlisting">
  791. HTTP/1.1
  792. UTF-8
  793. false
  794. null
  795. </pre>
  796.         </div>
  797.         <div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d4e327"></a>1.6.2.&nbsp;HTTP parameters beans</h3></div></div></div>
  798.            
  799.             <p><code class="interfacename">HttpParams</code> interface allows for a great deal of
  800.                 flexibility in handling configuration of components. Most importantly, new
  801.                 parameters can be introduced without affecting binary compatibility with older
  802.                 versions. However, <code class="interfacename">HttpParams</code> also has a certain
  803.                 disadvantage compared to regular Java beans:
  804.                     <code class="interfacename">HttpParams</code> cannot be assembled using a DI
  805.                 framework. To mitigate the limitation, HttpClient includes a number of bean classes
  806.                 that can used in order to initialize <code class="interfacename">HttpParams</code>
  807.                 objects using standard Java bean conventions.</p>
  808.             <pre class="programlisting">
  809. HttpParams params = new BasicHttpParams();
  810. HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
  811. paramsBean.setVersion(HttpVersion.HTTP_1_1);
  812. paramsBean.setContentCharset("UTF-8");
  813. paramsBean.setUseExpectContinue(true);
  814.  
  815. System.out.println(params.getParameter(
  816.     CoreProtocolPNames.PROTOCOL_VERSION));
  817. System.out.println(params.getParameter(
  818.     CoreProtocolPNames.HTTP_CONTENT_CHARSET));
  819. System.out.println(params.getParameter(
  820.     CoreProtocolPNames.USE_EXPECT_CONTINUE));
  821. System.out.println(params.getParameter(
  822.     CoreProtocolPNames.USER_AGENT));
  823. </pre>
  824.             <p>stdout &gt;</p>
  825.             <pre class="programlisting">
  826. HTTP/1.1
  827. UTF-8
  828. false
  829. null
  830. </pre>
  831.         </div>
  832.     </div>
  833.     <div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d4e337"></a>1.7.&nbsp;HTTP request execution parameters</h2></div></div></div>
  834.        
  835.         <p>These are parameters that can impact the process of request execution:</p>
  836.         <div class="itemizedlist"><ul type="disc"><li>
  837.                 <p>
  838.                     <b>'http.protocol.version':&nbsp;</b>
  839.                     defines HTTP protocol version used if not set explicitly on the request
  840.                         object. This parameter expects a value of type
  841.                             <code class="interfacename">ProtocolVersion</code>. If this parameter is not
  842.                         set HTTP/1.1 will be used.
  843.                 </p>
  844.             </li><li>
  845.                 <p>
  846.                     <b>'http.protocol.element-charset':&nbsp;</b>
  847.                     defines the charset to be used for encoding HTTP protocol elements. This
  848.                         parameter expects a value of type <code class="classname">java.lang.String</code>.
  849.                         If this parameter is not set <code class="literal">US-ASCII</code> will be
  850.                         used.
  851.                 </p>
  852.             </li><li>
  853.                 <p>
  854.                     <b>'http.protocol.content-charset':&nbsp;</b>
  855.                     defines the charset to be used per default for content body coding. This
  856.                         parameter expects a value of type <code class="classname">java.lang.String</code>.
  857.                         If this parameter is not set <code class="literal">ISO-8859-1</code> will be
  858.                         used.
  859.                 </p>
  860.             </li><li>
  861.                 <p>
  862.                     <b>'http.useragent':&nbsp;</b>
  863.                     defines the content of the <code class="literal">User-Agent</code> header. This
  864.                         parameter expects a value of type <code class="classname">java.lang.String</code>.
  865.                         If this parameter is not set, HttpClient will automatically generate a value
  866.                         for it.
  867.                 </p>
  868.             </li><li>
  869.                 <p>
  870.                     <b>'http.protocol.strict-transfer-encoding':&nbsp;</b>
  871.                     defines whether responses with an invalid
  872.                             <code class="literal">Transfer-Encoding</code> header should be rejected. This
  873.                         parameter expects a value of type <code class="classname">java.lang.Boolean</code>.
  874.                         If this parameter is not set invalid <code class="literal">Transfer-Encoding</code>
  875.                         values will be ignored.
  876.                 </p>
  877.             </li><li>
  878.                 <p>
  879.                     <b>'http.protocol.expect-continue':&nbsp;</b>
  880.                     activates <code class="literal">Expect: 100-Continue</code> handshake for the entity
  881.                         enclosing methods. The purpose of the <code class="literal">Expect:
  882.                             100-Continue</code> handshake is to allow the client that is sending
  883.                         a request message with a request body to determine if the origin server is
  884.                         willing to accept the request (based on the request headers) before the
  885.                         client sends the request body. The use of the <code class="literal">Expect:
  886.                             100-continue</code> handshake can result in a noticeable performance
  887.                         improvement for entity enclosing requests (such as <code class="literal">POST</code>
  888.                         and <code class="literal">PUT</code>) that require the target server's authentication.
  889.                            <code class="literal">Expect: 100-continue</code> handshake should be used with
  890.                        caution, as it may cause problems with HTTP servers and proxies that do not
  891.                        support HTTP/1.1 protocol. This parameter expects a value of type
  892.                            <code class="classname">java.lang.Boolean</code>. If this parameter is not set
  893.                        HttpClient will attempt to use the handshake.
  894.                </p>
  895.            </li><li>
  896.                <p>
  897.                    <b>'http.protocol.wait-for-continue':&nbsp;</b>
  898.                    defines the maximum period of time in milliseconds the client should spend
  899.                        waiting for a <code class="literal">100-continue</code> response. This parameter
  900.                        expects a value of type <code class="classname">java.lang.Integer</code>. If this
  901.                        parameter is not set HttpClient will wait 3 seconds for a confirmation
  902.                        before resuming the transmission of the request body.
  903.                </p>
  904.            </li></ul></div>
  905.    </div>
  906. </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="preface.html">Prev</a>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;<a accesskey="n" href="connmgmt.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Preface&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;Chapter&nbsp;2.&nbsp;Connection management</td></tr></table></div></body></html>