square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.09k stars 7.3k forks source link

java.net.SocketException: Connection reset but same request works with postman #2624

Closed kamilwlf closed 6 years ago

kamilwlf commented 6 years ago

I use retrofit 2.2 I do standard GET request. I get exception but same request works with postman

PM okhttp3.internal.platform.Platform log INFO: <-- HTTP FAILED: java.net.SocketException: Connection reset java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121) at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) at sun.security.ssl.InputRecord.read(InputRecord.java:503) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:961) at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:918) at sun.security.ssl.AppInputStream.read(AppInputStream.java:105) at okio.Okio$2.read(Okio.java:138) at okio.AsyncTimeout$2.read(AsyncTimeout.java:236) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210) at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:67) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179) at okhttp3.RealCall.execute(RealCall.java:63) at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)

JakeWharton commented 6 years ago

This is a normal exception from the socket which usually indicates that the server has closed the connection for some reason. This is not something that Retrofit has caused or has any control over.

kamilwlf commented 6 years ago

but when I use clear okhttp without retrofit it works. Of course same http request. Any ideas?

with okhttp:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("https://url?key=fdsfdsf&format=json")
  .get()
  .addHeader("Key", "key")
  .build();`

with retrofit:

retrofit = new Retrofit.Builder()
                    .baseUrl(NetworkConsts.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();

 @Headers("Key: " + KEY)
    @GET()
    Call<List<EnquirePojo>> get(@Url String url,
                                             @Query("key") String key,
                                             @Query("format") String format
    );
gustavogervasio commented 6 years ago

@kamilwlf did you find the problem?

kamilwlf commented 6 years ago

no I just changed library(I use clean OkHttp) because with retrofit for some request got java.net.SocketException. With clean OkHttp it works

Tin-Nguyen commented 6 years ago

Did you figure out the root cause already @gustavogervasio @kamilwlf , I got the same issue with HttpURLConnection

TobiasBuchholz commented 6 years ago

I had exactly the same problem. Turns out the issue seemed to be Java7, after switching to Java8 everything worked as expected.

rwmorey71 commented 6 years ago

I switch our Android app to Java 1.8 but this problem still exists.

GrimTimekeeper commented 5 years ago

I had this connection reset issue only sometimes when using a @QueryMap.

`@QueryMap(encoded=true) Map<String, String> args`

I was using a HashMap object defined as:

`HashMap<String, String> queryArgs = new HashMap<>()`

This worked for several cases where I had multiple pairs in the map, but it threw a connection reset every time in one particular case. The fix that cured the connection reset was to change my map definition to be:

`Map<String, String> queryArgs = new HashMap<>()`
Djek-grif commented 2 years ago

In my case it was some re-configuration on server side. Server guy just recreate ALB and new instance was using different version of TSL. So just try to use or remove "connectionSpecs" config with your OkHttpClient like:

    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
            List<ConnectionSpec> connectionSpecs = new ArrayList<>();
            connectionSpecs.add(ConnectionSpec.COMPATIBLE_TLS);
      //      clientBuilder.connectionSpecs(connectionSpecs);    <-- add or remove it
devoma commented 5 months ago

I had the same network issue. In my case, it turned out that the issue was caused by an enabled VPN. Once I disabled it, everything worked