HttpCore 教程(四)

(二)、阻塞式的Http协议处理

1、Http Service

HttpService 是一个基于阻塞式I/O模型,满足Http协议对服务端消息处理的基本要求的服务端协议处理器。

HttpService 依赖于 HttpProcessor来为所有传出消息生成强制性的协议头,对于所有的传入和传出消息应用常见的交叉消息转换,而Http请求处理器更关注应用程序特定内容的生成和处理。

示例代码:

public void httpServiceTest() {
        HttpProcessor processor = HttpProcessorBuilder.create()
                                    .add(new ResponseDate())
                                .add(new ResponseServer("MyServer-HTTP/1.1"))
                        .add(new ResponseContent())
                            .add(new ResponseConnControl())
                                        .build();
        //构造方法中需要传入一个HttpProcessorde对象
        HttpService service = new HttpService(processor, null);
        System.out.println(service);
    }

(1)、http请求处理器

HttpRequestHandler接口代表处理一组特定Http请求的过程,主要目的是为了响应给定的请求,而生成一个带有内容实体的响应对象,以便于将其发送给客户端。
一个典型的HttpRequestHandler示例代码如下:

public void httpRequestHandlerTest() {
        HttpRequestHandler mRequestHandler = new HttpRequestHandler() {

            @Override
            public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                    throws HttpException, IOException {
                response.setStatusCode(HttpStatus.SC_OK);
                response.setEntity(new StringEntity("important message",ContentType.TEXT_PLAIN));
            }
        };
    }

(2)、请求处理程序解析器

Http请求处理器通常由HttpRequestHandlerResolver来管理,它能将一个Http请求的URI与HttpRequestHandler匹配。
HttpCore包含一个基于简单模式匹配算法的HttpRequestHandlerResolver的实现。

HttpRequestHandlerRegistry支持三种格式:
- *
- <uri>*
- *<uri>

示例代码:

public void httpRequestHandlerResolverTest() {
        HttpProcessor processor = <...>

        HttpRequestHandler mHandler1 = <...>
        HttpRequestHandler mHandler2 = <...>
        HttpRequestHandler mHandler3 = <...>

        UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
        handlerMapper.register("/service/*", mHandler1);
        handlerMapper.register("*.do", mHandler2);
        handlerMapper.register("*", mHandler3);

        HttpService service = new HttpService(processor, handlerMapper);
    }

(3)、使用Http Service 处理请求

完成初始化和配置之后,HttpService能够用于执行和处理处于活动状态的Http连接上的请求。
HttpService#handleRequest()方法能够读入传入数据,生成相应的响应并将它发送给客户端,这个方法能够循环的处理一个持续连接上的多个请求。此方法在多线程中是==线程安全==的。只要协议拦截器和请求处理器是线程安全的,那么此方法就允许同时处理多个连接上的请求。
示例代码:

public void httpServiceTest() throws Exception {
        HttpProcessor processor = HttpProcessorBuilder.create()
                .add(new ResponseDate())
                .add(new ResponseServer())
                .add(new ResponseContent())
                .add(new ResponseConnControl())
                .build();
        //定义一个请求处理器
        HttpRequestHandler handler = new HttpRequestHandler() {

            @Override
            public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                    throws HttpException, IOException {
                response.setStatusCode(HttpStatus.SC_OK);
                response.setEntity(new StringEntity("message",ContentType.TEXT_PLAIN));
            }
        };

        UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
        //请求处理器与相应的uri对应
        handlerMapper.register("*", handler);

        HttpService httpService = new HttpService(processor, handlerMapper);

        HttpServerConnection connection = new DefaultBHttpServerConnection(8*1024);

        HttpContext context = new BasicHttpContext();

        boolean active = true;
        try {
            //循环处理同一个连接上的请求
            while(active && connection.isOpen()) {
                httpService.handleRequest(connection, context);
            }
        } finally {
            //关闭连接
            connection.shutdown();
        }
    }

2、Http请求执行器

HttpRequestExecutor是一个客户端的协议处理器,它是基于阻塞式I/O模型,实现了Http协议对客户端消息处理所提出的必要的要求。

HttpRequestExecutor依赖HttpProcessor对所有传入的消息产生强制性的协议头,对于所有传入和传出的消息应用通用交叉消息转换。

一旦执行请求并收到响应,应用程序就能实现HttpRequestExecutor之外的特定处理。
示例代码:

public void httpRequestExecutorTest() throws Exception {
        Socket socket = new Socket("127.0.0.1", 8080);
        DefaultBHttpClientConnection connection = new DefaultBHttpClientConnection(8*1024);
        connection.bind(socket);

        HttpProcessor processor = HttpProcessorBuilder.create()
                    .add(new RequestContent())
                    .add(new RequestConnControl())
                    .add(new RequestTargetHost())
                    .add(new RequestUserAgent())
                    .add(new RequestExpectContinue(true))
                    .build();

        HttpRequest request = new BasicHttpRequest("GET", "/",HttpVersion.HTTP_1_1);

        HttpCoreContext context = HttpCoreContext.create();

        HttpRequestExecutor httpRequestExecutor = new HttpRequestExecutor();

        httpRequestExecutor.preProcess(request, processor, context);
        HttpResponse response = httpRequestExecutor.execute(request, connection, context);
        httpRequestExecutor.postProcess(response, processor, context);

        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    }

注意:HttpRequestExecutor类的方法是==线程安全==的


之后内容见下一篇博客,写完之后附链接
之前内容见上一篇博客,HttpCore 教程(三)

猜你喜欢

转载自blog.csdn.net/venus14/article/details/79734666