使用码头HTTP客户端发送Https请求中的NullPointerException

Ali*_*jad 6 java https httpclient jetty-9

我正在使用Jetty HTTP客户端(v9.2.5)发送HTTP请求,对于HTTP请求{Post,Get,...}来说,它工作正常,但是当我发送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:642)
Run Code Online (Sandbox Code Playgroud)

我的功能是

private Object sendHttpPOSTRequest(String url, List<Header> headers,
        String content, HttpMethod method) {
    try {
        HttpClient client = new HttpClient();
        client.setMaxConnectionsPerDestination(16);
        client.setAddressResolutionTimeout(5000);
        client.setConnectTimeout(5000);
        client.setMaxRedirects(3);
        client.setFollowRedirects(true);
        client.setExecutor(_executor);
        client.start();
        Request req = client.POST(url).content(
                new StringContentProvider(content));
        if (headers != null)
            req = setHeaders(req, headers);
        req.header(HttpHeader.CONTENT_TYPE, "application/json");

        return req.send();

    } catch (Exception e) {
        e.printStackTrace();
        return "Failed";
    }

}
Run Code Online (Sandbox Code Playgroud)

怎么了?

小智 5

对于 HTTPS,您必须使用 httpclient 初始化,如下所示:

// Instantiate and configure the SslContextFactory
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();

// Instantiate HttpClient with the SslContextFactory
HttpClient httpClient = new HttpClient(sslContextFactory);

// Configure HttpClient, for example:
httpClient.setFollowRedirects(false);

// Start HttpClient
httpClient.start();
Run Code Online (Sandbox Code Playgroud)