【问题标题】:Implementing custom HTTP methods with HttpCore使用 HttpCore 实现自定义 HTTP 方法
【发布时间】:2011-08-06 13:26:52
【问题描述】:

我是 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

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

解决方案:我需要实现一个 HttpRequestFactory。 例如:

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

【问题讨论】:

    标签: java http extension-methods apache-httpcomponents


    【解决方案1】:

    看起来您不需要重写 doService()。你宁愿需要为你的方法实现一个处理程序并确保

        handler = this.handlerResolver.lookup(requestURI);
    

    返回您的处理程序。我认为你这样做了,但由于某种原因,你的处理程序没有找到。我敢打赌你没有正确注册。

    【讨论】:

    • 感谢您的回复。我在 HttpRequestHandlerRegistry 中注册了一个处理程序。所有标准方法都由它处理,但如果我发送非标准 http 方法(例如 curl -X FOO localhost),它不会被调用。 HttpService.doService() 也没有到达。
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 2020-05-02
    • 2011-05-30
    • 2012-05-09
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多