【发布时间】:2021-03-29 06:52:45
【问题描述】:
我遇到异常,响应通过 OkHttpClient 的 https 休息服务调用
我正在使用库 com.squareup.okhttp3:okhttp-ws:3.4.2。
javax.net.ssl.SSLException:无法识别的 SSL 消息,明文连接? 在 sun.security.ssl.InputRecord.handleUnknownRecord(Unknown Source)
File certFile = resourceLoader.getResource("classpath:" + certKey).getFile();
try {
sslContext = SSLContextBuilder.create()
.loadKeyMaterial(certFile, certKeyPassword.toCharArray(), certKeyPassword.toCharArray())
.loadTrustMaterial(certFile, certKeyPassword.toCharArray()).build();
SSLSocketFactory sslSocketFactory = sslContext().getSocketFactory();
RequestBody formBody = new FormBody.Builder()
.add("<key>", "<value>")
.build();
OkHttpClient okHttpClient = new OkHttpClient.Builder().socketFactory(sslSocketFactory).build();
Request request = new Request.Builder()
.url(https URL)
.post(formBody)
.addHeader("Content-Type", "application/json")
.build();
LOG.info("Request passed: "+request);
response = okHttpClient.newCall(request).execute();
当我使用 http URL 时,它会将 https URL 发送到交互应用程序。 但在日志中,我在执行调用之前打印,它只显示 http URL
我需要让它与 https URL 一起使用。
如果需要任何其他信息,请告诉我。
【问题讨论】:
标签: java http ssl https okhttp