hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Httpcomponents Wiki] Update of "HttpClientTutorial" by OlegKalnichevski
Date Tue, 31 Mar 2009 13:53:52 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpcomponents Wiki" for change notification.

The following page has been changed by OlegKalnichevski:
http://wiki.apache.org/HttpComponents/HttpClientTutorial

------------------------------------------------------------------------------
  };
  
  byte[] response = httpclient.execute(httpget, handler);
+ }}}
+ 
+ === HTTP execution context ===
+ 
+     Originally HTTP has been designed as a stateless, response-request oriented protocol.
However, real world applications often need to be able to persist state information through
several logically related request-response exchanges. In order to enable applications to maintain
a processing state HttpClient allows HTTP requests to be executed within a particular execution
context, referred to as HTTP context. Multiple logically related requests can participate
in a logical session if the same context is reused between consecutive requests. HTTP context
functions similarly to java.util.Map<String, Object>. It is simply a collection of arbitrary
named values. Application can populate context attributes prior to a request execution or
examine the context after the execution has been completed. 
+ 
+     In the course of HTTP request execution HttpClient adds the following attributes to
the execution context:
+ 
+     * 'http.connection' - HttpConnection instance representing the actual connection to
the target server.
+ 
+     * 'http.target_host' - HttpHost instance representing the connection target.
+ 
+     * 'http.proxy_host' - HttpHost instance representing the connection proxy, if used.
+ 
+     * 'http.request' - HttpRequest instance representing the actual HTTP request.
+ 
+     * 'http.response' - HttpResponse instance representing the actual HTTP response.
+ 
+     * 'http.request_sent' - Boolean object representing the flag indicating whether the
actual request has been fully transmitted to the connection target.
+     
+     For instance, in order to determine the final redirect target, one can examine the value
of the  'http.target_host' attribute after the request execution:
+     
+ {{{
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ 
+ HttpContext localContext = new BasicHttpContext();
+ HttpGet httpget = new HttpGet("http://www.google.com/");
+ 
+ HttpResponse response = httpclient.execute(httpget, localContext);
+ 
+ HttpHost target = (HttpHost) localContext.getAttribute(
+         ExecutionContext.HTTP_TARGET_HOST);
+ 
+ System.out.println("Final target: " + target);
+ 
+ HttpEntity entity = response.getEntity();
+ if (entity != null) {
+     entity.consumeContent();
+ }
+ }}}
+ 
+ stdout >
+ 
+ {{{
+ Final target: http://www.google.ch
  }}}
  
  === Exception handling ===
@@ -464, +509 @@

  
      In some situations HTTP request execution fail to complete within the expected time
frame due to high load on the target server or too many concurrent requests executed on the
client side. In such cases it may be necessary to terminate the request prematurely and unblock
the execution thread blocked in a I/O operation. HTTP requests being executed by HttpClient
can be aborted at any stage of execution by invoking HttpUriRequest#abort method. This method
is thread-safe and can be called from any thread. When an HTTP request is aborted its execution
thread blocked in an I/O operation is guaranteed to unblock by throwing a InterruptedIOException
  
- == HTTP execution context ==
- 
-     Originally HTTP has been designed as a stateless, response-request oriented protocol.
However, real world applications often need to be able to persist state information through
several logically related request-response exchanges. In order to enable applications to maintain
a processing state HttpClient allows HTTP requests to be executed within a particular execution
context, referred to as HTTP context. Multiple logically related requests can participate
in a logical session if the same context is reused between consecutive requests.        
- 
-     HTTP context functions similarly to java.util.Map. It is simply a collection of arbitrary
named values. Application can populate context attributes prior to a request execution or
examine the context after the execution has been completed. 
- 
-     In the course of HTTP request execution HttpClient adds the following attributes to
the execution context:
- 
-     * '''http.connection''' - HttpConnection instance representing the actual connection
to the target server.
- 
-     * '''http.target_host''' - HttpHost instance representing the connection target.
- 
-     * '''http.proxy_host''' - HttpHost instance representing the connection proxy, if used.
- 
-     * '''http.request''' - HttpRequest instance representing the actual HTTP request.
- 
-     * '''http.response''' - HttpResponse instance representing the actual HTTP response.
- 
-     * '''http.request_sent''' - Boolean object representing the flag indicating whether
the actual request has been fully transmitted to the connection target.
-   
  == HTTP protocol interceptors ==
  
     HTTP protocol customization using custom protocol interceptors

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message