使用HttpCore实现自定义HTTP方法

时间:2011-04-15 18:35:17

标签: java http extension-methods apache-httpcomponents

我是Java的新手,我希望Apache HttpCore库有一些方向。

我编写了一个简单的服务器,并希望实现一些自定义HTTP方法。我已经阅读了几次文档,但还是没能弄明白。

看起来在HttpService.doService()中引发了501 Not Implemented,但是覆盖该方法不起作用。我的请求处理程序没有被调用。

任何帮助将不胜感激。

感谢。

以下是我所得到的要点:

    ServerSocket serverSocket;
    HttpParams params; 
    HttpService httpService;
    serverSocket = new ServerSocket(8000);

    params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
    params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024);
    params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
    params.setParameter(CoreProtocolPNames.ORIGIN_SERVER, "?");

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());

    HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
    registry.register("*", new HttpRequestHandler() {
        public void handle(HttpRequest request, HttpResponse response,
                HttpContext context) throws HttpException, IOException {
            System.out.println(request.getRequestLine().toString());

        }
    });

    httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());
    httpService.setParams(params);
    httpService.setHandlerResolver(registry);

    Socket socket = serverSocket.accept();
    DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
    conn.bind(socket, params);

    HttpContext context = new BasicHttpContext();
    httpService.handleRequest(conn, context);
    socket.close();
    conn.shutdown();

    serverSocket.close();

响应:

# curl -X FOO -i http://127.0.0.1:8000
HTTP/1.0 501 Not Implemented
Content-Length: 26
Content-Type: text/plain; charset=US-ASCII
Connection: Close

FOO method not supported

请求行不会写入System.out,除非方法是GET,POST等。

解决方案:我需要实现一个HttpRequestFactory。 e.g:

    DefaultHttpServerConnection conn = new DefaultHttpServerConnection() {
        @Override
        public DefaultHttpRequestFactory createHttpRequestFactory() {
            return new DefaultHttpRequestFactory() {
                @Override
                public HttpRequest newHttpRequest(final RequestLine requestline) {
                    return new BasicHttpRequest(requestline);
                }
                @Override
                public HttpRequest newHttpRequest(final String method, final String uri) {
                    return new BasicHttpRequest(method, uri);
                }
            };
        }
    };

1 个答案:

答案 0 :(得分:0)

看起来你不需要覆盖doService()。您需要为您的方法实现处理程序并确保

    handler = this.handlerResolver.lookup(requestURI);

返回你的处理程序。我带你做到了,但由于某种原因,找不到你的处理程序。我打赌你没有正确注册。