一、问题现象
最近在用okHttp处理http请求调用,编写jmeter压测脚本时,出现一个奇怪的问题,idea中可以正常调用。但是打成jar包后,在jmeter中去使用时,则调不通,报错:
clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on jdk 9+, 但是发现自己安装的jdk版本其实就是jdk8。
二、解决方案
查了半天,大概有两种解决办法:
1,使用更低版本的jdk
2,使用更高版本的okHttp(4.3.0版本及以上)
查了一下我项目工程中的OKhttp的版本,果然版本比较低,是3.x版本的。因此使用方法2,项目工程maven里面将okhttp的依赖版本改为4.3.0版本:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.0</version>
</dependency>
这样之后再次运行,不再报错:not supported on jdk 9+,但是报另外一个新的错:java.lang.NoSuchFieldError: Companion。
项目pom.xml文件中,鼠标右键》maven》Show Dependencies,查看各个依赖间的关系图:
可以看到okhttp与okio关联,网上查了一下原因,是底层依赖 okhttp 与okio的版本不兼容导致,okhttp是 v4.3.0,而okio的版本还是比较低的版本v1.14.0,因此会出现上述问题:
解决办法就是pom.xml文件中使用更高版本的okio, 比如:V2.8.0
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>2.8.0</version>
</dependency>
此后又碰到另外一个问题,当使用jmeter3.2版本时,用上面的方法已经不会报错了。但是当我换成jmeter 5.4.3版本后,还是要继续报:not supported on jdk 9+的错误。
因此我再尝试下面的做法,然后就不报错了。
新建一个类SSLSocketClient,代码如下:
import javax.net.ssl.*;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;
public class SSLSocketClient {
//获取这个SSLSocketFactory
public static SSLSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, getTrustManager(), new SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//获取TrustManager
private static TrustManager[] getTrustManager() {
return new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
}
//获取HostnameVerifier
public static HostnameVerifier getHostnameVerifier() {
return (s, sslSession) -> true;
}
public static X509TrustManager getX509TrustManager() {
X509TrustManager trustManager = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
trustManager = (X509TrustManager) trustManagers[0];
} catch (Exception e) {
e.printStackTrace();
}
return trustManager;
}
}
然后OkHttpClient初始化时设置sslSocketFactory和hostnameVerifier,代码如下:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
.build();
完整的HttpUtils工具类代码如下:文章来源:https://www.toymoban.com/news/detail-731932.html
import okhttp3.*;
import core.SSLSocketClient; //导入上面写的SSLSocketClient类,SSLSocketClient类是放在工程中的core文件夹下
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class HttpUtils {
private final static MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private static OkHttpClient client;
private static final Integer DEFAULT_TIMEOUT = 10;
static {
client = new OkHttpClient.Builder()
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
.build();
}
public static Response post(String url, Object param, Map<String, String> headers) throws IOException{
if (param instanceof String){
return post(url, (String) param, headers);
} else if (param instanceof Map){
return post(url, (Map<String, String>) param, headers);
}
else {
throw new IOException("没有对的参数类型,请检查");
}
}
private static Response post(String url, String param, Map<String, String> headers) throws IOException{
RequestBody requestBody = RequestBody.create(MEDIA_TYPE, param);
Request.Builder builder = new Request.Builder().url(url);
for (String key : headers.keySet()) {
builder.addHeader(key, headers.get(key));
}
Request request = builder.post(requestBody).build();
return client.newCall(request).execute();
}
private static Response post(String url, Map<String, String> param, Map<String, String> headers) throws IOException{
FormBody.Builder bodyBuilder = new FormBody.Builder();
for (String key: param.keySet()){
bodyBuilder.add(key, param.get(key));
}
RequestBody requestBody = bodyBuilder.build();
Request.Builder builder = new Request.Builder().url(url);
for (String key : headers.keySet()) {
builder.addHeader(key, headers.get(key));
}
Request request = builder.post(requestBody).build();
return client.newCall(request).execute();
}
}
=================================================================================================
以上就是本次的全部内容,如果对你有帮助,麻烦点个赞+收藏+关注,一键三连啦~ 欢迎关注下方我的公众号:程序员杨叔,各类文章都会第一时间在上面发布,持续分享各类测试开发知识干货,你的支持就是作者更新最大的动力。文章来源地址https://www.toymoban.com/news/detail-731932.html
到了这里,关于okhttp报错:clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on jdk 9+的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!