[英]How to download file from HTTP url that redirects to HTTPS url on Android?
[英]OkHttpClient redirects http URL to Https and gives SSLException with https URL
我遇到异常,响应通过 OkHttpClient 的 https rest 服务调用
我正在使用库 com.squareup.okhttp3:okhttp-ws:3.4.2。
javax.net.ssl.SSLException:无法识别的 SSL 消息,明文连接? 在 sun.security.ssl.InputRecord.handleUnknownRecord(未知来源)
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 ZE6B391A8D2C45D45903DZ3 发送到正在交互的应用程序5。 但是在日志中,我在执行调用之前正在打印,它只显示 http URL
我需要使它与 https URL 一起工作。
如果需要任何其他信息,请告诉我。
您不小心将 sslSocketFactory 传递给 socketFactory。
OkHttpClient okHttpClient = new OkHttpClient.Builder().socketFactory(sslSocketFactory).build();
你要
OkHttpClient okHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory, trustManager).build();
您必须使用一个非常旧的 OkHttp 版本,因为这是专门检查多年的。
/**
* Sets the socket factory used to create connections. OkHttp only uses the parameterless
* [SocketFactory.createSocket] method to create unconnected sockets. Overriding this method,
* e. g., allows the socket to be bound to a specific local address.
*
* If unset, the [system-wide default][SocketFactory.getDefault] socket factory will be used.
*/
fun socketFactory(socketFactory: SocketFactory) = apply {
require(socketFactory !is SSLSocketFactory) { "socketFactory instanceof SSLSocketFactory" }
if (socketFactory != this.socketFactory) {
this.routeDatabase = null
}
this.socketFactory = socketFactory
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.