IOException: unexpected end of stream on Connection solution

Used in the project is Retorfit + Rxjava network requests, requests a login interface request unreasonable, but the server has not received any request log, looked at the log, the method reported this error IOException: unexpected end of stream on Connection .
I go online find the find the cause of this problem has generally look at two types but mine is the second reason:
1, the first to see the explanation of the problem on github: the error occurs when OkHttp try to reuse a connection that is in FIN_WAIT2 state in server, because the server keep_alive timeout is lesser than the client timeout. then I found a place that could go wrong in the code, timeout setting my okhttp is 100, and the server is generally 60 temporary fix this ever happened .
2, IOException: unexpected end of stream on the reasons Connection is produced and okhttp version and have a relationship, you need to add a addHeader in interceptor ( "Connection", "close" ).
Worded as follows:

if(Build.VERSION.SDK !=null&&Build.VERSION.SDK_INT >13)

        {
            urlConnect.setRequestProperty("Connection", "close");
        }

        或者重新写一个拦截器如:class  NetInterceptor   implements  Interceptor

        {
            @Overridepublic Responseintercept(Chain chain)throws IOException {
            Request request = chain.request().newBuilder().addHeader("Connection", "close").build();
            returnchain.proceed(request);
        }
        }

        OkHttpClient client = newOkHttpClient.Builder().addNetworkInterceptor(new NetInterceptor()).build();
    }

This occurs because it plainly is why the server and client in tcp link, increasing addHeader ( "Connection", "close") will post when the request has been dealt cut off, after each request creates a new tcp link. I went online to find a description about this, request and reponse header are likely to appear in a header field in connection http1.1, the meaning of this header is when the client and server communications link for how long to process. In the http1.1, client and server support long links to the other side are the default, if client use http1.1 agreement, but do not want to use long link, you need to specify a value of close connection in the header; if the server side also I do not want to support long link, in response also need to clearly explain the connection is close.

Reproduced in: https: //www.jianshu.com/p/649c13deee04

Guess you like

Origin blog.csdn.net/weixin_33883178/article/details/91303882