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 a java.util.Map<String, Object>. It is simply a collection of arbitrary named values. An application can populate context attributes prior to request execution or examine the context after the execution has been completed.

HttpContext can contain arbitrary objects and therefore may be unsafe to share between multiple threads. It is recommended that each thread of execution maintains its own context.

In the course of HTTP request execution HttpClient adds the following attributes to the execution context:

  • HttpConnection instance representing the actual connection to the target server.

  • HttpHost instance representing the connection target.

  • HttpRoute instance representing the complete connection route

  • HttpRequest instance representing the actual HTTP request. The final HttpRequest object in the execution context always represents the state of the message exactly as it was sent to the target server. Per default HTTP/1.0 and HTTP/1.1 use relative request URIs. However if the request is sent via a proxy in a non-tunneling mode then the URI will be absolute.

  • HttpResponse instance representing the actual HTTP response.

  • java.lang.Boolean object representing the flag indicating whether the actual request has been fully transmitted to the connection target.

  • RequestConfig object representing the actual request configuation.

  • java.util.List<URI> object representing a collection of all redirect locations received in the process of request execution.

One can use HttpClientContext adaptor class to simplify interractions with the context state.

HttpContext context = <...>
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpHost target = clientContext.getTargetHost();
HttpRequest request = clientContext.getRequest();
HttpResponse response = clientContext.getResponse();
RequestConfig config = clientContext.getRequestConfig();

Multiple request sequences that represent a logically related session should be executed with the same HttpContext instance to ensure automatic propagation of conversation context and state information between requests.

In the following example the request configuration set by the initial request will be kept in the execution context and get propagated to the consecutive requests sharing the same context.

CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(1000)
.build(); HttpGet httpget1 = new HttpGet("http://localhost/1");
httpget1.setConfig(requestConfig);
CloseableHttpResponse response1 = httpclient.execute(httpget1, context);
try {
HttpEntity entity1 = response1.getEntity();
} finally {
response1.close();
}
HttpGet httpget2 = new HttpGet("http://localhost/2");
CloseableHttpResponse response2 = httpclient.execute(httpget2, context);
try {
HttpEntity entity2 = response2.getEntity();
} finally {
response2.close();
}

最新文章

  1. ArrayList、Vector、LinkedList源码
  2. hdu-1213-How Many Tables
  3. 学习OpenCV——Surf简化版
  4. Linux网络管理
  5. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作
  6. jQuery 、js 设置 显示隐藏
  7. 消息推送SignalR
  8. android中onStartActivityForResult无返回值问题
  9. QDialog弹出一个窗口,改变窗口大小
  10. Android Studio打包APK时出现 is not translated in &quot;en&quot; (English) [MissingTranslation]
  11. The currently displayed page contains invalid values.
  12. xlrd模块
  13. react material-ui 添加jss插件
  14. icpc2018-焦作-D-几何模拟
  15. Android launchMode SingleTask newIntent 的问题
  16. linux 释放内存及查看内存命令
  17. Ext.js多文件选择上传,
  18. swift NSdata 转换 nsstring
  19. C语言 &#183; 乘法运算
  20. react-redux: modal

热门文章

  1. java commons-lang 工具包 逃脱工具 转unicode 及其他
  2. java选项及系统属性
  3. MVC神韵---你想在哪解脱!(十八)
  4. WebView自适应并嵌套在ScrollView里
  5. 有关java中static关键的重写问题
  6. IT项目管理工具总结(转载)
  7. Redis命令小细节
  8. Asp.net 使用正则和网络编程抓取网页数据(有用)
  9. Java中for循环以及循环中标签
  10. iOS开发——UI篇OC篇&amp;layoutSubviews和drawRect