我使用以下处理程序在 Netty 中创建了一个 HTTPServer:
public class HttpRouterServerHandler extends SimpleChannelInboundHandler<HttpRequest> {
public void channelRead0(ChannelHandlerContext ctx, HttpRequest req) {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
return;
}
System.out.println(req.toString());
HttpResponse res = /* create the response here */
flushResponse(ctx, req, res);
}
我使用 http.client 在 Python 中发送 PUT 请求:
json_data = /* constructJSON content */
connection = http.client.HTTPConnection("localhost", 8080)
connection.request("PUT", "/proto/api/send", json_data)
response = self.connection.getresponse()
connection.close()
由于某种原因,我无法获取请求的 BODY(json 内容)。原因似乎是 my HttpRequest
is not a FullHttpRequest
,所以我无法得到它content()
如果我打印我的请求内容,我有类似的内容:
DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
PUT /proto/api/send HTTP/1.1
Host: localhost:8080
Accept-Encoding: identity
content-length: 47
此外,我尝试用 FullHttpRequest 替换我的处理程序的 HttpRequest,但在这种情况下,Netty 服务器不再响应,这导致 Python 中出现异常:
public class HttpRouterServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
return;
}
System.out.println(req.toString());
HttpResponse res = /* create the response here */
flushResponse(ctx, req, res);
}
我的错误在哪里?