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();
    }
}
时间: 2024-09-28 16:13:49

HttpClient(4.3.5) - HTTP Protocol Interceptors的相关文章

HttpClient简易介绍

http://lovelaomao.blogbus.com/tag/HttpClient/ 1.HttpClient的功能 基于标准,纯正java,实现了http1.0和1.1. 在一个可扩展的OO框架内,实现了HTTP的全部方法(GET, POST, PUT, DELETE, HEAD,OPTIONS,and TRACE) 支持HTTPS(ssl上的HTTP)的加密操作 透明地穿过HTTP代理建立连接 通过CONNECT方法,利用通过建立穿过HTTP代理的HTTPS连接 利用本地Java so

HttpClient Coder Example

Example 1: HttpClient httpClient = new HttpClient();                 httpClient.getHostConfiguration().setHost("mail.qq.com");                 HttpMethod http = new PostMethod("http://m537.mail.qq.com/cgi-bin/frame_html?sid=oYV8g1dfxab5VHk3

httpcomponents-client-4.4.x

Chapter 1. Fundamentals Prev     Next Chapter 1. Fundamentals 1.1. Request execution The most essential function of HttpClient is to execute HTTP methods. Execution of an HTTP method involves one or several HTTP request / HTTP response exchanges, usu

httpcomponents-client-ga(4.5)

Chapter 1. Fundamentals Prev     Next Chapter 1. Fundamentals 1.1. Request execution The most essential function of HttpClient is to execute HTTP methods. Execution of an HTTP method involves one or several HTTP request / HTTP response exchanges, usu

巧用apache httpcore-nio实现android推送服务

1. 背景 Android推送服务应用非常广泛,一般有轮询.SMS推送.IP推送几种实现方式.由于轮询的即时性不高.SMS推送需要不菲的费用,所以一般采取IP推送.由于google的IP推送平台C2DM国内被屏蔽,国内涌现很多优秀的推送平台如个推.极光推送.由于实现推送服务有一定技术难度,很多移动互联网应用直接租用这些平台服务,达到快速拓展业务的目标. 但是在一些企业和行业应用场合,限制手机接入互联网,不能采用互联网推送平台,就必须实现自己的推送服务.国内流行的方案是采用开源的androidpn

微信支付后台获取openid

@RequestMapping(value = "/postOpenid", method = RequestMethod.POST) @ResponseBody public String getOpenid(@RequestParam(value = "code", required = false) String code, HttpServletRequest request, ModelMap model) throws IOException { Sys

Http Clinet使用

Http Client是个apache下的一个开源包,用于使用http协议访问服务的java代码编写. Http Client的主要功能: (1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等) (2)支持自动转向 (3)支持 HTTPS 协议 (4)支持代理服务器等 使用Http Client需要的包:commons-httpclient.jar,commons-logging.jar,commons-codec-1.x.jar HttpClient 可以在http://

Android项目使用Eclipse进行单元测试

Android项目如果每次都整个调试的话,要加载UI,会等很长时间.所以单元测试就显得很方便了. 要进行单元测试,首先得修改下AndroidManifest.xml文件.在Instrument标签里点右侧的Add按钮.然后在弹出的窗口双击 Instrument.然后在右侧就会多出一些东西赖.先选择第二行的Target package 的Browse……,选择要测试的包.或者要测试的类所属的包.我这里选的事 com.example.scrollview 包,然后再选择第一行 Name 后的Brow

ASIHttpRequest封装

ASIHttpRequest是一个很好的库,不过直接使用稍嫌麻烦,下面就尝试来封装一下吧! 思路:每次请求时,需要创建一个ASIHttpRequest对象,设置它的属性(url,delegate,postValue,requestMethod等)之后即可开始异步请求.所以我们可以创建一个类,对外提供一个请求方法,结构如下: @interface RequestService : NSObject + (void)requestWithUrlString:(NSString *)urlString