2015-10-15 105 views
2

我正在使用Jetty開發我的客戶端應用程序端。 我沒有在服務器部分使用Jetty。 我需要在客戶端配置使用Jetty客戶端發送「https」請求嗎?Jetty:如何在Jetty客戶端使用SSL

這就是我對HTTP客戶端做:

httpClient = new HttpClient(); 
// Configure HttpClient 
httpClient.setFollowRedirects(false); 

httpClient.start(); 

Request request = httpClient.newRequest(url); 
//code 
httpClient.stop(); 

我得到這個異常,如果我嘗試使用發送請求「https」開頭:

java.util.concurrent.ExecutionException: java.lang.NullPointerException 
    at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118) 
    at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101) 
    at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:653) 
    at egm.httpClient.jetty.TestBackend.POST(TestBackend.java:204) 
    at egm.httpClient.jetty.TestStep.execute(TestStep.java:77) 
    at egm.httpClient.jetty.TestSuite.execute(TestSuite.java:57) 
    at egm.httpClient.jetty.TestLauncher.main(TestLauncher.java:139) 
Caused by: java.lang.NullPointerException 
    at org.eclipse.jetty.io.ssl.SslClientConnectionFactory.newConnection(SslClientConnectionFactory.java:57) 
    at org.eclipse.jetty.client.AbstractHttpClientTransport$ClientSelectorManager.newConnection(AbstractHttpClientTransport.java:187) 
    at org.eclipse.jetty.io.ManagedSelector.createEndPoint(ManagedSelector.java:411) 
    at org.eclipse.jetty.io.ManagedSelector.access$1600(ManagedSelector.java:56) 
    at org.eclipse.jetty.io.ManagedSelector$CreateEndPoint.run(ManagedSelector.java:587) 
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213) 
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.execute(ExecuteProduceConsume.java:101) 
    at org.eclipse.jetty.io.ManagedSelector.run(ManagedSelector.java:136) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572) 
    at java.lang.Thread.run(Unknown Source) 

回答

6

你必須通過SslContextFactory進入HttpClient是這樣的:

HttpClient httpClient = new HttpClient(new SslContextFactory()); 

你甚至可以配置SslContextFactory通過信任所有或給它一個密鑰存儲:

new SslContextFactory(true); 
new SslContextFactory("/path/to/.keystore");