使用 SSL 的 Jetty HTTP 客户端

发布于 2024-07-23 19:40:08 字数 1137 浏览 6 评论 0原文

我正在关注 Jetty HttpClient 示例,但我无法获取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());

I am following the Jetty HttpClient Example, but I am unable to get an SSL connection working. When I connect using a proxy, it throws a "Not Implemented" exception. When I don't use a proxy, it doesn't return anything.

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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅人 2024-07-30 19:40:08

码头 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 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 { ...
糖粟与秋泊 2024-07-30 19:40:08

是的,很奇怪,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 中清楚地标记这一点。 另一种选择是尝试为他们实现该功能并将其作为补丁提交回来。

Yeah weird, the source code for the Jetty-Client's SelectConnector looks like the following:

 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");
 }

so the functionality doesn't exist at present - at least in the version I'm using (6.1.16) for using a proxy in this kind of way. It's also the same in the milestone Jetty 7 version (I found after downloading the source code).

I suggest your try a different client - check out Apache HttpClient:

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

The Jetty developers should really have marked this clearly in the Javadocs. another alternative is to implementinghave a go at implementing the feature for them and submitting it back as a patch.

遗心遗梦遗幸福 2024-07-30 19:40:08

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

try ProxyHandler (jetty 7) which handle connect-command for tunneling https-connection (via proxy)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文