Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty: How to use SSL in Jetty client side

Tags:

java

ssl

jetty

I am using Jetty to develop my client application side. I am not using Jetty in the server part. What I need to configure on the client side to be able send "https" request using the Jetty client?

That is what I do for HTTP client:

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

httpClient.start();

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

I got this exception if I try to send request using "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)
like image 630
sabrina2020 Avatar asked Dec 18 '22 23:12

sabrina2020


1 Answers

Since jetty-client >= 10

HttpClient supports HTTPS requests out-of-the-box like a browser does.


Prior jetty-client 9

In order to perform HTTPS requests, you should create first a SslContextFactory.Client

You have to pass a SslContextFactory into the HttpClient like this:

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

You can even configure the SslContextFactory by trusting all or give it a keystore:

new SslContextFactory(true);
new SslContextFactory("/path/to/.keystore");
like image 155
Roald Bankras Avatar answered Dec 21 '22 11:12

Roald Bankras