4

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)
sabrina2020
  • 2,102
  • 3
  • 25
  • 54

2 Answers2

14

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");
Dennis C
  • 24,511
  • 12
  • 71
  • 99
Roald Bankras
  • 564
  • 4
  • 14
5

this is what worked for me with jetty-11

SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
sslContextFactory.setTrustAll(true); // you might want to think about this first


ClientConnector clientConnector = new ClientConnector();
clientConnector.setSslContextFactory(sslContextFactory);

HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
httpClient.start();

for explanation, check out the official documentation here https://www.eclipse.org/jetty/documentation/jetty-11/programming-guide/index.html#pg-client-http-configuration-tls

mwikya
  • 86
  • 1
  • 3