使用 SSL 的 Jetty HTTP 客户端
我正在关注 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
码头 v7.4.1:
Jetty v7.4.1:
是的,很奇怪,Jetty-Client 的 SelectConnector 的源代码如下所示:
所以该功能目前不存在 - 至少在我正在使用的版本(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:
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.
尝试 ProxyHandler (jetty 7),它处理用于隧道 https 连接的连接命令(通过代理)
try ProxyHandler (jetty 7) which handle connect-command for tunneling https-connection (via proxy)