json - 无法使用 netty 在 http 客户端中读取响应

标签 json httprequest netty httpresponse

我能够通过与服务器建立的连接写入我的 http 请求,但我无法读取来自服务器的响应(不确定是否有任何响应)。我如何检查它然后读取它?我的服务器正在返回 json 作为响应..

客户端代码:

public class NettyClient {



      public static void main(String[] args) throws Exception {
           URI uri = new URI("http://myurl.com/v1/v2?param1=value1");          
           String scheme = uri.getScheme() == null? "http" : uri.getScheme();
           String host = uri.getHost();
            int port = 443;

            boolean ssl = "https".equalsIgnoreCase(scheme);

         // Configure the client.
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                Bootstrap b = new Bootstrap();
                b.group(group)
                 .channel(NioSocketChannel.class)
                 .handler(new NettyClientInitializer(false));
                // Make the connection attempt.
                Channel ch = b.connect(host, port).sync().channel();
                // Prepare the HTTP request.
                HttpRequest request = new DefaultHttpRequest(
                        HttpVersion.HTTP_1_1, HttpMethod.GET, "http://myurl.com/v1/v2?param1=value1");
                request.headers().set(HttpHeaders.Names.HOST, host);
                request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);            
                //request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

                // Send the HTTP request.
                ch.writeAndFlush(request);

                // Wait for the server to close the connection.
                ch.closeFuture().sync();
            } finally {
                // Shut down executor threads to exit.
                group.shutdownGracefully();
            }
      }
}

初始化代码:
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {

    private final boolean ssl;

    public NettyClientInitializer(boolean ssl) {
        this.ssl = ssl;
    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        // Create a default pipeline implementation.
        ChannelPipeline p = ch.pipeline();

        p.addLast("log", new LoggingHandler(LogLevel.INFO));
        // Enable HTTPS if necessary.
        /*
        if (ssl) {
            SSLEngine engine =
                SecureChatSslContextFactory.getClientContext().createSSLEngine();
            engine.setUseClientMode(true);

            p.addLast("ssl", new SslHandler(engine));
        }
*/
        p.addLast("codec", new HttpClientCodec());

        // Remove the following line if you don't want automatic content decompression.
       // p.addLast("inflater", new HttpContentDecompressor());

        // Uncomment the following line if you don't want to handle HttpChunks.
        //p.addLast("aggregator", new HttpObjectAggregator(1048576));
        p.addLast("handler", new NettyClientHandler());
    }
}

处理程序代码:
public class NettyClientHandler extends SimpleChannelInboundHandler<HttpObject> {

    @Override
    public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        if (msg instanceof HttpResponse) {
            HttpResponse response = (HttpResponse) msg;

            System.out.println("STATUS: " + response.getStatus());
            System.out.println("VERSION: " + response.getProtocolVersion());
            System.out.println();

            if (!response.headers().isEmpty()) {
                for (String name: response.headers().names()) {
                    for (String value: response.headers().getAll(name)) {
                        System.out.println("HEADER: " + name + " = " + value);
                    }
                }
                System.out.println();
            }

            if (HttpHeaders.isTransferEncodingChunked(response)) {
                System.out.println("CHUNKED CONTENT {");
            } else {
                System.out.println("CONTENT {");
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent content = (HttpContent) msg;

            System.out.print(content.content().toString(CharsetUtil.UTF_8));
            System.out.flush();

            if (content instanceof LastHttpContent) {
                System.out.println("} END OF CONTENT");
            }
        }
    }

    @Override
    public void exceptionCaught(
            ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }


}

在控制台中,我可以看到正在写入的请求,但在刷新请求之后, channel 将处于非事件状态,然后被取消注册。

可能是因为我的查询参数没有被发送或 json 问题,还是我的代码中遗漏了什么?

最佳答案

问题可能在于您在端口 443 上连接到服务器

int port = 443;
...
Channel ch = b.connect(host, port).sync().channel();

但是您的 NettyClientInitializer 不支持 SSL(它已从管道中注释掉)并且您构建它就好像 SSL 没有被使用一样
new NettyClientInitializer(false)

如果您将端口更改为 80,您的代码工作正常。

关于json - 无法使用 netty 在 http 客户端中读取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065168/

相关文章:

java - 跨多个 ServerBootstrap 使用相同的线程池是否安全?

python - 序列化和反序列化用户定义类中的对象

c# - 如何将多个参数传递给 web api

javascript - 从 ios 数据中去除 html 标签

httprequest - Symfony kernel.controller事件发送响应

C Socket写HTTP GET请求: error 400

python - Arduino Heroku 请求

netty - 如何有效地从 ByteBuf 中获取一个短数组?

json - 在模式 rdd 中分解 json 数组

java - 如何管理netty中发送数据的客户端列表