首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MockWebserver Retrofit SSLHandshakeException:连接关闭

MockWebserver Retrofit SSLHandshakeException:连接关闭
EN

Stack Overflow用户
提问于 2022-11-30 04:06:54
回答 1查看 22关注 0票数 0

我正在编写一个UI测试,使用Espresso、Dagger2、Retrofit和MockWebserver来模拟API响应。我跟随本教程由黄伟典介绍。 -> 这里有完整的github源。我不使用SSL钉扎,所以我远程处理它。但是当我试图改变webmockserver拦截器时,我遇到了麻烦。日志显示错误行在这里chain.proceed(请求)。

代码语言:javascript
复制
class DebugUrlInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var request: Request = chain.request()
        if (MockServerManager.shouldMockApi(request.url.encodedPath)) {
            val newUrl: HttpUrl = request.url.newBuilder()
                .scheme(MockServerManager.HTTPS_SCHEME)
                .host(MockServerManager.HOST)
                .port(MockServerManager.port)
                .build()

            request = request.newBuilder()
                .url(newUrl)
                .headers(request.headers)
                .method(request.method, request.body)
                .build()
        }
        return chain.proceed(request)
    }
}

我确实使用了x509Trustmanager,但它仍然不起作用。

代码语言:javascript
复制
    @Provides
    @Named("unsafe")
    fun provideUnSafeOkHttpClient(okHttpClientBuilder: OkHttpClient.Builder): OkHttpClient {
        val x509TrustManager = @SuppressLint("CustomX509TrustManager")
        object : X509TrustManager {
            @SuppressLint("TrustAllX509TrustManager")
            override fun checkClientTrusted(
                chain: Array<out X509Certificate>?,
                authType: String?
            ) {
            }

            @SuppressLint("TrustAllX509TrustManager")
            override fun checkServerTrusted(
                chain: Array<out X509Certificate>?,
                authType: String?
            ) {
            }

            override fun getAcceptedIssuers(): Array<X509Certificate> {
                return arrayOf()
            }
        }

        val trustManagers = arrayOf<TrustManager>(x509TrustManager)

        val sslContext = SSLContext.getInstance("SSL")
        sslContext.init(null, trustManagers, java.security.SecureRandom())

        okHttpClientBuilder.sslSocketFactory(sslContext.socketFactory, x509TrustManager)
        okHttpClientBuilder.hostnameVerifier { _, _ -> true }

        return okHttpClientBuilder.build()
    }

全错误日志:

代码语言:javascript
复制
  javax.net.ssl.SSLHandshakeException: connection closed
 W      at com.android.org.conscrypt.SSLUtils.toSSLHandshakeException(SSLUtils.java:362)
 W      at com.android.org.conscrypt.ConscryptEngineSocket.doHandshake(ConscryptEngineSocket.java:240)
 W      at com.android.org.conscrypt.ConscryptEngineSocket.startHandshake(ConscryptEngineSocket.java:217)
 W      at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
 W      at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
 W      at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
 W      at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
 W      at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
 W      at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
 W      at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
 W      at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
 W      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W      at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
 W      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W      at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
 W      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W      at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
 W      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W      at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:221)
 W      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W      at com.jinjerkeihi.api.DebugUrlInterceptor.intercept(DebugUrlInterceptor.kt:29)
 W      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W      at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
 W      at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
 W      at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
 W      at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:46)
 W      at io.reactivex.Observable.subscribe(Observable.java:12246)
 W      at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35)
 W      at io.reactivex.Observable.subscribe(Observable.java:12246)
 W      at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
 W      at io.reactivex.Single.subscribe(Single.java:3575)
 W      at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
 W      at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
 W      at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)

以前有人这样做过吗?我已经花了一天的时间

EN

回答 1

Stack Overflow用户

发布于 2022-11-30 08:01:11

问题在于:

代码语言:javascript
复制
val newUrl: HttpUrl = request.url.newBuilder()
                .scheme(MockServerManager.HTTPS_SCHEME)
                .host(MockServerManager.HOST)
                .port(MockServerManager.port)
                .build()

它需要是HTTP_SCHEME而不是HTTPS_SCHEME

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74622788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档