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的更多相关文章

  1. Apache HttpComponents Custom protocol interceptors通过拦截器自定义压缩

    /* * ==================================================================== * Licensed to the Apache S ...

  2. HTTPClient to use http/https protocol to send request

    使用了spring boot, gradle, commons-httpcomponent3. 目前httpclient 已经有了版本4. https://github.com/lvfe/httpCl ...

  3. Table of Contents - HttpClient

    HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...

  4. HttpClient学习记录-系列1(tutorial)

    1. HttpClient使用了facade模式,如何使用的? 2. HTTP protocol interceptors使用了Decorator模式,如何使用的? URIBuilder respon ...

  5. httpclient调用https

    httpclient调用https报错: Exception in thread "main" java.lang.Exception: sun.security.validato ...

  6. java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory

    当不需要任何证书访问https时,java中先实现一个MySSLProtocolSocketFactory类忽略证书的信任 package com.tgb.mq.producer.utils; imp ...

  7. HttpClient Coder Example

    Example 1:   HttpClient httpClient = new HttpClient();                 httpClient.getHostConfigurati ...

  8. httpcomponents-client-4.4.x

    Chapter 1. Fundamentals Prev     Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...

  9. httpcomponents-client-ga(4.5)

    http://hc.apache.org/httpcomponents-client-ga/tutorial/html/   Chapter 1. Fundamentals Prev     Next ...

随机推荐

  1. C#Windows窗体界面设计_01_绘制三角函数_附强制类型转换

    binzhouweichao@163.com 今天开始学习C#windows窗体界面设计. 首先说一下类型转换. 参考http://www.csharpwin.com/csharpspace/6848 ...

  2. java中动态代理

    一.在java中怎样实现动态代理 1.我们要有一个接口,还要有一个接口的实现类,而这个实现类呢就是我们要代理的对象 接口: package org.dynamicproxy.test; public ...

  3. mysql case when 条件过滤

    [1].[代码] 使用CASE WHEN进行字符串替换处理 跳至 [1] [2] [3] [4] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...

  4. Netty4.x分析

    官网定义: netty是一个异步.事件驱动的网络应用框架,用于快速开发可维护的.高性能的服务端和客户端程序. 原理分析  Architecture Overview 网络模型:netty采用了Reac ...

  5. Hash Table构建

    get-item e:\test\* |format-table @{name="aa";expression={$_.name.tostring().split(".& ...

  6. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  7. Codeforces Gym 100733H Designation in the Mafia flyod

    Designation in the MafiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...

  8. delphi 14 内容编辑

    撤销 重做 - 复制 剪切 粘贴 删除 - 全选 不选       ///编辑 ///撤销    WebBrowser1.ExecWB(OLECMDID_REDO ,1); ///重做    WebB ...

  9. C++中new与delete问题学习

    一.new char与delete问题 . 问题程序 [cpp] view plaincopy #include <iostream> using namespace std; void ...

  10. PHP做好防盗链的基本思想 防盗链的设置方法

    盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率.受益者不提供资源或提供 ...