简体   繁体   English

使用HttpCore实现自定义HTTP方法

[英]Implementing custom HTTP methods with HttpCore

I'm new to Java and am hoping for some direction with Apache HttpCore library. 我是Java的新手,并且希望对Apache HttpCore库有所帮助。

I've written a simple server, and would like to implement a few custom HTTP methods. 我已经编写了一个简单的服务器,并希望实现一些自定义HTTP方法。 I've gone through the docs a few time but haven't been able to figure it out. 我已经阅读了几次文档,但无法弄清楚。

It looks like the 501 Not Implemented is raised in HttpService.doService() , but overriding that method doesn't work. 看起来HttpService.doService()中引发了501 Not Implemented,但是覆盖该方法无效。 My request handler doesn't get called. 我的请求处理程序没有被调用。

Any help will be appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

Here's the gist of what I've got: 这是我所拥有的要点:

    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();

Response: 响应:

# 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

Request line isn't written to System.out unless method is GET, POST, etc. 除非方法是GET,POST等,否则请求行不会写入System.out。

SOLUTION: I needed to implement a HttpRequestFactory. 解决方案:我需要实现HttpRequestFactory。 eg: 例如:

    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);
                }
            };
        }
    };

It doesn't look like you need to override doService(). 看起来您不需要重写doService()。 You rather need to implement a handler for you method and make sure 您宁愿为您的方法实现一个处理程序,并确保

    handler = this.handlerResolver.lookup(requestURI);

returns your handler. 返回您的处理程序。 I take you did that, but for some reason your handler is not found. 我认为您是这样做的,但是由于某种原因,找不到您的处理程序。 I bet you didn't register it properly. 我敢打赌,您没有正确注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM