【问题标题】:Netty - sending http requests over sslNetty - 通过 ssl 发送 http 请求
【发布时间】:2013-09-18 20:00:35
【问题描述】:

我正在尝试让客户端/服务器程序通过 ssl 交换 http 消息。首先,我创建了使用 DefaultHttpRequest 成功交换 http 请求的客户端和服务器程序。发送请求的代码如下所示:

    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "https://localhost:8443");

    ChannelBuffer buf = ChannelBuffers.copiedBuffer(line, "UTF-8");
    request.setContent(buf);

    request.setHeader(HttpHeaders.Names.HOST, host);
    request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    request.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/xml");
    request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, Integer.toString(buf.capacity()));

    ChannelFuture writeFuture = channel.write(request);

客户端管道工厂包含以下内容:

pipeline.addLast("decoder", new HttpResponseDecoder());
pipeline.addLast("encoder", new HttpRequestEncoder());

// and then business logic.
...

服务器管道工厂包含以下内容:

pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());

// and then business logic.

....

到目前为止一切顺利。客户端发送,服务器接收并解码请求。使用正确的数据调用我的处理程序上的 messageReceived 方法。

为了启用 SSL,我从 SecureChat 示例中提取了一些代码并添加到客户端和服务器管道工厂:

对于服务器:

SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
engine.setUseClientMode(false);

pipeline.addLast("ssl", new SslHandler(engine));

// On top of the SSL handler, add the text line codec.
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
        8192, Delimiters.lineDelimiter()));

对于客户:

SSLEngine engine = SecureChatSslContextFactory.getClientContext().createSSLEngine();
engine.setUseClientMode(true);

pipeline.addLast("ssl", new SslHandler(engine));

// On top of the SSL handler, add the text line codec.
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
        8192, Delimiters.lineDelimiter()));

现在,当我从客户端发送请求时,服务器上似乎没有发生任何事情。当我启动应用程序时,服务器似乎已连接(调用了 channelConnected),但是当我发送消息时,没有任何数据到达服务器(从未调用过 messageReceived)。

我在做什么明显有问题吗?这是https应该工作的方式吗?还是有其他通过 ssl 发送 http 请求的方法?

谢谢, 韦兹恩

【问题讨论】:

    标签: http ssl netty


    【解决方案1】:

    您需要在客户端调用 SslHandler.handshake()。再次检查其中的示例。

    【讨论】:

      【解决方案2】:

      糟糕,我似乎从 SecureChat 示例中复制和粘贴了太多内容。

      删除 DelimiterBasedFrameDecoder 似乎可以解决问题。

      【讨论】:

        猜你喜欢
        • 2013-11-13
        • 1970-01-01
        • 2013-01-15
        • 2011-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-25
        相关资源
        最近更新 更多