使用 HttpCore 实现自定义 HTTP 方法
我是 Java 新手,希望能获得 Apache HttpCore 库的一些指导。
我编写了一个简单的服务器,并且想实现一些自定义 HTTP 方法。我已经浏览了几次文档,但一直无法弄清楚。
看起来 501 Not Implemented 是在 HttpService 中引发的.doService(),但重写该方法不起作用。我的请求处理程序没有被调用。
任何帮助将不胜感激。
谢谢。
以下是我得到的要点:
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);
}
};
}
};
I'm new to Java and am hoping for some direction with Apache HttpCore library.
I've written a simple server, and would like to implement a few custom HTTP methods. 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. 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.
SOLUTION: I needed to implement a 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);
}
};
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您不需要重写 doService()。您需要为您的方法实现一个处理程序并确保
返回您的处理程序。我认为您做到了,但由于某种原因找不到您的处理程序。我敢打赌你没有正确注册它。
It doesn't look like you need to override doService(). You rather need to implement a handler for you method and make sure
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.