2

我有一个 HttpServer 和一个 HttpClient。如果我在任一侧使用 Netty 的 HttpServerCodec 和 HttpObjectAggregator,我都会收到错误消息。

服务器代码

HttpServer server = HttpServer.create(opts -> opts.listen(8092).afterChannelInit(channelInit -> {
    ChannelPipeline pipeline = channelInit.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(64*1024));
}));
server.newRouter(routes -> {
    routes.post("/xmlvend", postHandler());
}).block(Duration.ofSeconds(30));

BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> postHandler() {
    return (req, resp) -> {
        req.requestHeaders().entries()
           .forEach(entry -> log.debug(String.format("header [%s=>%s]", entry.getKey(), entry.getValue())));
        return resp.sendString(req.receive().asByteArray().take(1).log("received").flatMap(data -> {
            return Flux.just("Hello");
        }));
    };
}

客户

HttpClient client = HttpClient.create(opts -> opts.connect(host, port).poolResources(pool).afterChannelInit(serverEndPoint));
ByteBuf reqContent = getTestRequest();
DefaultFullHttpRequest reqHeader = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, url, reqContent);
Mono<HttpClientResponse> rsp = client.request(HttpMethod.POST, url, (req) -> req.chunkedTransfer(true).addHeader("Content-Length", Integer.toString(reqHeader.content().capacity())).sendHeaders().send(Flux.just(reqContent)/*clientRequest -> clientRequest.onNext(reqHeader.content())*/));
rsp.subscribe().block();

serverEndPoint 还包含 HttpServerCodec 和 ObjectAggregator。我尝试使用 HttpClientCodec,在管道中不使用任何内容尝试发送 DefaultFullHttpRequest。要么我得到了异常,要么在客户端只读取了 1024 个字节。我需要发送一个超过 1024 字节的分块请求。

谢谢

4

0 回答 0