gpt4 book ai didi

java - Jetty:如何在 Jetty 客户端使用 SSL

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:33 24 4
gpt4 key购买 nike

我正在使用 Jetty 开发我的客户端应用程序。我没有在服务器部分使用 Jetty。我需要在客户端配置什么才能使用 Jetty 客户端发送“https”请求?

这就是我为 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)

最佳答案

自 jetty-client >= 10

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


之前的 jetty-client 9

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

您必须像这样将 SslContextFactory 传递到 HttpClient 中:

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

您甚至可以通过信任所有或给它一个 keystore 来配置 SslContextFactory:

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

关于java - Jetty:如何在 Jetty 客户端使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com