【问题标题】:NullPointerException In Send Https Request With jetty Http Client使用码头 Http 客户端发送 Https 请求中的 NullPointerException
【发布时间】:2015-05-05 11:50:20
【问题描述】:

我正在使用 Jetty HTTP Client (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)

我的功能是

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";
    }

}

怎么了?

【问题讨论】:

  • 你的客户端在哪里初始化?
  • 你的 _executor 是在哪里定义的?
  • 在类的顶部定义 private ExecutorService _executor = Executors.newCachedThreadPool();

标签: java https httpclient jetty-9


【解决方案1】:

对于 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();

【讨论】:

    【解决方案2】:

    你应该使用(见https://www.eclipse.org/jetty/documentation/current/http-client.html):

    // 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();
    

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 2014-11-03
      • 2021-11-09
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多