Skip to content Skip to sidebar Skip to footer

Http Failed: Java.io.ioexception: Unexpected End Of Stream Exception While Making Https Request

We are previously using http apis and now we have migrated to https, with same code we are facing the exception HTTP FAILED: java.io.IOException: unexpected end of stream (this is

Solution 1:

I just had this happen and figured out a workaround. This was caused by the use of the HttpLoggingInterceptor. Through debugging into the okhttp code I found that it was throwing that error because the length of the body did not match what the content length header in the response was. If it receives a content header it uses a FixedLengthSource and that will throw the "unexpected end of stream" error when it is read. You can see this in the source https://github.com/square/okhttp/blob/okhttp_3.9.x/okhttp/src/main/java/okhttp3/internal/http1/Http1Codec.java openResponseBody method

As a workaround I added a response interpreter to my builder that strips out the content-length header which causes it to use an UnknownLengthSource.

privatestaticfinalInterceptorREWRITE_CONTENT_LENGTH_INTERCEPTOR=newInterceptor() {
    @Overridepublic Response intercept(Interceptor.Chain chain)throws IOException {
      ResponseoriginalResponse= chain.proceed(chain.request());
      return originalResponse.newBuilder().removeHeader("Content-Length")
              .build();
    }
  }; 

Post a Comment for "Http Failed: Java.io.ioexception: Unexpected End Of Stream Exception While Making Https Request"