HttpClient(4.3.5) - HTTP Protocol Interceptors
The HTTP protocol interceptor is a routine that implements a specific aspect of the HTTP protocol. Usually protocol interceptors are expected to act upon one specific header or a group of related headers of the incoming message, or populate the outgoing message with one specific header or a group of related headers. Protocol interceptors can also manipulate content entities enclosed with messages - transparent content compression / decompression being a good example. Usually this is accomplished by using the 'Decorator' pattern where a wrapper entity class is used to decorate the original entity. Several protocol interceptors can be combined to form one logical unit.
Protocol interceptors can collaborate by sharing information - such as a processing state - through the HTTP execution context. Protocol interceptors can use HTTP context to store a processing state for one request or several consecutive requests.
Usually the order in which interceptors are executed should not matter as long as they do not depend on a particular state of the execution context. If protocol interceptors have interdependencies and therefore must be executed in a particular order, they should be added to the protocol processor in the same sequence as their expected execution order.
Protocol interceptors must be implemented as thread-safe. Similarly to servlets, protocol interceptors should not use instance variables unless access to those variables is synchronized.
This is an example of how local context can be used to persist a processing state between consecutive requests:
CloseableHttpClient httpclient = HttpClients.custom()
.addInterceptorLast(new HttpRequestInterceptor() { public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
AtomicInteger count = (AtomicInteger) context.getAttribute("count");
request.addHeader("Count", Integer.toString(count.getAndIncrement()));
} })
.build(); AtomicInteger count = new AtomicInteger(1);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAttribute("count", count); HttpGet httpget = new HttpGet("http://localhost/");
for (int i = 0; i < 10; i++) {
CloseableHttpResponse response = httpclient.execute(httpget, localContext);
try {
HttpEntity entity = response.getEntity();
} finally {
response.close();
}
}
HttpClient(4.3.5) - HTTP Protocol Interceptors的更多相关文章
- Apache HttpComponents Custom protocol interceptors通过拦截器自定义压缩
/* * ==================================================================== * Licensed to the Apache S ...
- HTTPClient to use http/https protocol to send request
使用了spring boot, gradle, commons-httpcomponent3. 目前httpclient 已经有了版本4. https://github.com/lvfe/httpCl ...
- Table of Contents - HttpClient
HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...
- HttpClient学习记录-系列1(tutorial)
1. HttpClient使用了facade模式,如何使用的? 2. HTTP protocol interceptors使用了Decorator模式,如何使用的? URIBuilder respon ...
- httpclient调用https
httpclient调用https报错: Exception in thread "main" java.lang.Exception: sun.security.validato ...
- java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory
当不需要任何证书访问https时,java中先实现一个MySSLProtocolSocketFactory类忽略证书的信任 package com.tgb.mq.producer.utils; imp ...
- HttpClient Coder Example
Example 1: HttpClient httpClient = new HttpClient(); httpClient.getHostConfigurati ...
- httpcomponents-client-4.4.x
Chapter 1. Fundamentals Prev Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...
- httpcomponents-client-ga(4.5)
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/ Chapter 1. Fundamentals Prev Next ...
随机推荐
- AngularJS 学习笔记一
ng-app 指令 ng-app 指令定义了 AngularJS 应用程序的 根元素. ng-app 指令在网页加载完毕时会自动引导(自动初始化)应用程序. 稍后您将学习到 ng-app 如何通过一个 ...
- C#Windows窗体界面设计_01_绘制三角函数_五点作图法
binzhouweichao@163.com Visual Stutio 2010 C#开发环境 五点作图绘制三角函数,以正弦函数为例,选取一个周期[0, 2π]上的五个特殊的点,也就是横坐标0, π ...
- Dynamically loading an external JavaScript or CSS file
原文: Dynamically loading an external JavaScript or CSS file 通过javascript动态加载css文件和javascript文件,主要是通 ...
- C#学习笔记(十五):预处理指令
C#和C/C++一样,也支持预处理指令,下面我们来看看C#中的预处理指令. #region 代码折叠功能,配合#endregion使用,如下: 点击后如下: 条件预处理 条件预处理可以根据给出的条件决 ...
- Oracle 的 INSERT ALL和INSERT FIRST
描述性的东西就不来了,搞技术的,最喜欢实在的实例.通过下面的例子,大家很快就能明白insert all 与 insert first 的功能,比文字描述更通俗易懂. 一.INSERT ALL 不带条件 ...
- cocos2d-x 让精灵按照自己设定的运动轨迹行动
转自:http://blog.csdn.net/ufolr/article/details/7447773 在cocos2d中,系统提供了CCMove.CCJump.CCBezier(贝塞尔曲线)等让 ...
- 一种基于Welch's t检验的二元关系挖掘
现实中常常需要挖掘两种因素之间的关联,Welch's t检验很适合其中的nomial-numerical的关系挖掘.比如天气状况对销量的影响,或者天气情况对交通流量的影响等等.我们可以按照下雨/不下雨 ...
- 配置Windows Update,补丁更新
配置Windows Update更新下载及安装方式: #NotificationLevel说明: # 0:未配置,不会对当前设置进行更改 # 1:从不检查更新 # 2:检查更新,但是让我选择是否下载和 ...
- php 算法之切割数组,不用array_chunk(),算法之二,取数组的差值,不用array_diff()
用php写算法切割数组,不用array_chunk();算法例如以下所看到的. <?php //$array 数组 //$size 每一个数组的个数 //每一个数组元素是否默认键值 functi ...
- HttpRequest
#ifndef __HTTP_REQUEST_H__ #define __HTTP_REQUEST_H__ #include "cocos2d.h" #include " ...