我正在使用Apache HttpClient 5和Conscrypt来通过SSL同时执行HTTP 2.0请求,如下所示:
final SSLContext sslContext;
try {
sslContext = SSLContexts.custom()
.setProvider(Conscrypt.newProvider())
.build();
} catch (Exception e) {
// ... omitted for brevity
}
final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(new ConscriptClientTlsStrategy(sslContext))
.build();
final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
.setConnectionManager(cm)
.build();
try {
httpclient.start();
HttpHost host = new HttpHost("www.wikidata.org");
final HttpClientContext clientContext = HttpClientContext.create();
final SimpleHttpRequest request = SimpleHttpRequests.GET.create(host, "/w/api.php?action=wbsearchentities&search=Washington");
request.addHeader("Accept-Charset", charset);
FutureCallback<SimpleHttpResponse> callback = // ... omitted for brevity
httpclient.execute(SimpleRequestProducer.create(request),
SimpleResponseConsumer.create(),
clientContext,
callback);
}
catch (Exception e) {
// ... omitted for brevity
}
在单元测试中运行代码成功。但是,如果作为在Tomcat v8中运行的Web应用程序的一部分运行,则会在调用
Conscrypt.newProvider()
时引发以下异常:message: java.lang.UnsatisfiedLinkError: Failed creating temp file (null)
。看来是特权问题。有人可以指出在这种情况下我应该配置什么来解决问题吗? 最佳答案
您需要在类路径中设置conscrypt-openjdk-uber-1.4.2.jar而不是conscrypt-openjdk-1.4.2.jar,希望这能解决您的问题,因为uber jar将具有conscrypt所需的所有依赖项。
还可以使用Http2AsyncClientBuilder而不是HttpAsyncClients进行HTTP2多路复用。