繁体   English   中英

码头 HTTP 客户端 SSL

[英]Jetty HTTP Client with SSL

我正在关注Jetty HttpClient Example ,但我无法使 SSL 连接正常工作。 当我使用代理连接时,它会抛出“未实现”异常。 当我不使用代理时,它不会返回任何东西。

HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.setProxy(new Address("www.example.com", 80));
client.start();

// create the exchange object, which lets you define where you want to go
// and what you want to do once you get a response
ContentExchange exchange = new ContentExchange()
{
  // define the callback method to process the response when you get it
  // back
  protected void onResponseComplete() throws IOException
  {
    super.onResponseComplete();
    String responseContent = this.getResponseContent();

    // do something with the response content
    System.out.println(responseContent);
  }
};

exchange.setMethod("GET");
exchange.setURL("https://www.example.com");
exchange.setScheme(HttpSchemes.HTTPS_BUFFER);

// start the exchange
client.send(exchange);
exchange.waitForDone();
System.err.println("Response status: " + exchange.getResponseStatus());

码头 v7.4.1:

if (dest.isSecure()) {
    if (dest.isProxied()) {
        SSLEngine engine=newSslEngine(channel); ep = new ProxySelectChannelEndPoint(channel, selectSet, key, _sslBuffers, engine, (int)_httpClient.getIdleTimeout());
    } else { ...

是的很奇怪,Jetty-Client 的 SelectConnector 的源代码如下所示:

 if (dest.isProxied()) {
    String connect = HttpMethods.CONNECT+" "+dest.getAddress()+HttpVersions.HTTP_1_0+"\r\n\r\n";
    // TODO need to send this over channel unencrypted and setup endpoint to ignore the 200 OK response.    
    throw new IllegalStateException("Not Implemented");
 }

所以该功能目前不存在 - 至少在我使用的版本(6.1.16)中以这种方式使用代理。 在里程碑的Jetty 7版本中也是如此(我是下载源码后发现的)。

我建议您尝试使用不同的客户端 - 查看 Apache HttpClient:

http://hc.apache.org/httpclient-3.x/

Jetty 开发人员真的应该在 Javadocs 中清楚地标记这一点。 另一种选择是实现 go 为他们实现该功能并将其作为补丁提交回来。

试试 ProxyHandler (jetty 7),它处理用于隧道 https 连接的连接命令(通过代理)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM