• HttpClient(4.3.5)


    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();
        }
    }
  • 相关阅读:
    对“一道有趣的题目”的解答
    在Debian中使用Notify
    Debian服务器安装详细流程
    Lighttpd1.4.20源码分析之插件系统(1)plugin结构体和插件接口
    Lighttpd1.4.20源码分析之etag.c(h) HTTP/1.1中的Etag域
    Lighttpd1.4.20源码分析之bitset.c(h) 位集合的使用
    伸展树
    GCC中的弱符号与强符号
    Lighttpd1.4.20源码分析之fdevent系统(3) 使用
    Lighttpd1.4.20源码分析之插件系统(3)PLUGIN_TO_SLOT宏
  • 原文地址:https://www.cnblogs.com/huey/p/5721273.html
Copyright © 2020-2023  润新知