【问题标题】:Retrofit Https call give Connection reset改造 Https 调用给连接重置
【发布时间】:2016-12-22 16:04:43
【问题描述】:

我试图在我的spring boot 项目中使用retrofit 来访问https 调用,但每次我运行它时,它都会给出

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:209) ~[na:1.8.0_102]
    at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_102]
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) ~[na:1.8.0_102]
    at sun.security.ssl.InputRecord.read(InputRecord.java:503) ~[na:1.8.0_102]
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) ~[na:1.8.0_102]
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) ~[na:1.8.0_102]
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) ~[na:1.8.0_102]
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) ~[na:1.8.0_102]
    at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:242) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:200) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:174) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:196) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:132) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:101) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) ~[okhttp-3.5.0.jar:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) ~[okhttp-3.5.0.jar:na]
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179) ~[okhttp-3.5.0.jar:na]
    at okhttp3.RealCall.execute(RealCall.java:63) ~[okhttp-3.5.0.jar:na]
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:174) ~[retrofit-2.1.0.jar:na]

我的实现如下:

public static final Retrofit retrofit = new Retrofit.Builder().baseUrl(Utils.BASE_URL)
            .client(new OkHttpClient().newBuilder().retryOnConnectionFailure(true).connectTimeout(15, TimeUnit.SECONDS)
                    .readTimeout(50, TimeUnit.SECONDS).build())
            .addConverterFactory(GsonConverterFactory.create()).build();

和电话:

Service service = service.retrofit.create(Service.class);
Call<myObject> call = service.callService();
call.execute().body();

更新 我将-Djavax.net.debug=ssl:handshake:verbose 添加到我的jvm 并得到以下输出:

Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
Allow unsafe renegotiation: false
Allow legacy hello messages: true
Is initial handshake: true
Is secure renegotiation: false
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 for TLSv1.1
%% No cached client session
*** ClientHello, TLSv1.2
RandomCookie:  GMT: 1482447462 bytes = { 90, 39, 6, 215, 111, 143, 240, 128, 196, 112, 178, 59, 97, 245, 196, 32, 59, 178, 217, 232, 1, 178, 221, 33, 93, 26, 143, 120 }
Session ID:  {}
Cipher Suites: [TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA]
Compression Methods:  { 0 }
Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1}
Extension ec_point_formats, formats: [uncompressed]
Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA256withDSA, SHA224withECDSA, SHA224withRSA, SHA224withDSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA
Extension server_name, server_name: [type=host_name (0), value=api-test]
Extension renegotiation_info, renegotiated_connection: <empty>
***
http-nio-8080-exec-1, WRITE: TLSv1.2 Handshake, length = 189
http-nio-8080-exec-1, handling exception: java.net.SocketException: Connection reset
http-nio-8080-exec-1, SEND TLSv1.2 ALERT:  fatal, description = unexpected_message
http-nio-8080-exec-1, WRITE: TLSv1.2 Alert, length = 2
http-nio-8080-exec-1, Exception sending alert: java.net.SocketException: Broken pipe
http-nio-8080-exec-1, called closeSocket()
http-nio-8080-exec-1, called close()
http-nio-8080-exec-1, called closeInternal(true)

【问题讨论】:

  • 你有解决办法吗?我遇到了同样的错误。
  • 我找到的解决方案是使用 Curl,它有效,但从未让它与改造一起使用

标签: java sockets spring-boot retrofit2


【解决方案1】:

在我的情况下,和你的非常相似,这是 TLS 协议级别的问题。

服务器正在关闭连接,因为不接受 TLS 1.2 下的协议。我的改造客户端在 Java 7 下运行,默认情况下使用 TLS 1.1,所以服务器总是拒绝所有连接。

解决方案是将 TLS 1.2 设置为 Java VM 客户端连接的默认协议。如果您更改为 Java 1.8+,问题会自动修复

【讨论】:

  • 这个答案需要那些迫使安卓开发者永远支持旧API版本的公司非常关注! (在这种情况下 API developer.android.com/training/articles/security-config
【解决方案2】:

将网址从 http 更改为 https 为我解决了这个问题。 我使用的是http://testthesms.000webhostapp.com/......而不是https://testthesms.000webhostapp.com...... 虽然其他一些请求通过 http。

【讨论】:

    【解决方案3】:

    在我的例子中,我使用 retrofit 发送一个对象,并接收带有插入数据库的 id 值的相同对象。

    问题是我在我的服务器端类中添加了两个属性,但是我忘记在我的客户端类中添加这两个属性(我们知道交换数据的两个类必须相同且具有相同的属性)并且我得到了

    套接字超时异常:连接被重置

    希望这对某人有所帮助

    【讨论】:

      【解决方案4】:

      将您的网址更改为 HTTPS。它对我有用

      【讨论】:

      猜你喜欢
      • 2016-02-21
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      相关资源
      最近更新 更多