2009-06-16 120 views
1

我正在关注Jetty HttpClient Example,但我无法获得SSL连接的工作。当我使用代理进行连接时,它会引发“未实现”异常。当我不使用代理时,它不会返回任何内容。使用SSL的Jetty HTTP客户端

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

哪个码头的客户端版本您使用的? – Jon 2009-06-16 19:09:30

回答

2

码头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 { ... 
1

呀怪异,为码头客户端的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/

码头开发商要真有明确的Javadoc标注这一点。另一种选择是实施它们,为他们实现功能并将其作为补丁提交回去。

1

尝试其处理ProxyHandler(码头7)连接命令为隧道HTTPS连接(通过代理)