32

I have web service URL, it working fine. It gives the JSON data.

When I am using HttpURLConnection and InputStream, I am getting this error:

java.io.IOException: unexpected end of stream on
Connection{comenius-api.sabacloud.com:443, proxy=DIRECT
hostAddress=12.130.57.1
cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1}
(recycle count=0)

My code:

try {
    URL url = new URL("https://comenius-api.sabacloud.com/v1/people/username=" + username + ":(internalWorkHistory)?type=internal&SabaCertificate=" + certificate);

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    InputStream ist = con.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(ist));

    while ((singleLine = reader.readLine()) != null) {
        data = data + singleLine;
        Log.e("Response", data);
    }

} catch (Exception e) {
    e.printStackTrace();
}

How to fix this?

Boken
  • 4,825
  • 10
  • 32
  • 42

12 Answers12

44

I had the same problem using OKHttp3. The problem was that I didn't close the connection for each request and for the client the same connection was available and for the server not, for this reason the server returns a error.

The solution is indicating to each request to close the connection when it is finished. You have to add a flag in the header to indicate this. In OKHttp3 is like this:

Request request = new Request.Builder()
                             .url(URL)
                             .header("Connection", "close")
                             ...
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
Radost
  • 845
  • 7
  • 11
  • 4
    This is the answer. It works perfectly to me!!. I had been looking for hours. – Ivan Oct 04 '18 at 09:41
  • I had a method with Request object passed as a parameter. Have to use the following code snippet instead >> request.addHeader("Connection", "close"); – Tarun May 09 '19 at 12:48
  • It's perfect answer! – Atif Mahmood May 22 '19 at 10:38
  • Adding the `Connection: Close` header gave me another error (`javax.net.ssl.SSLException: Write error: ssl=0x7b730be688: I/O error during system call, Broken pipe`) – Cullub Mar 19 '20 at 08:40
  • Even after adding this, server randomly throws error as stated by OP. – Sharp Edge Dec 01 '20 at 06:44
  • Quoting https://stackoverflow.com/a/58217819/483520 > Recommendation to close connections on the client side should be considered just as a workaround, keep in mind that it degrades overall performance. – Nolan Amy Apr 30 '21 at 22:47
  • 1
    Wooooow! This answer saved our day! We use Apache NiFi for integration with TOTVS Protheus ERP. NiFi runs the InvokeHTTP process, and Protheus was returning this error message. So we added a property named "connection" with the content "close" on InvokeHTTP processor attributes, and it worked like a charm! Thank you so much! – Guilherme Bigois Jun 22 '23 at 15:00
11

I encountered this problem today. And it turns out that it is the server fault, as in the server throwed an error and shut down as it is parsing the request.

Check your backend, if it is not yours, notify the owner of that server

grandia
  • 709
  • 7
  • 20
11

"Keepalive makes it difficult for the client to determine where one response ends and the next response begins" 1

It seems that the issue is caused by a collision on reusing alive connection under 2 cases:

  1. server doesn't send Content-Length in response headers

  2. (streaming content case, so no Content-Length can be used) server doesn't use Chunked transfer encoding

So if you observed the exception, sniff http headers (e.g. at Android Studio Profiler tool). If you will see in response header both

  • "Connection: keep-alive"

and no

  • "Content-Length: ***" or "Transfer-Encoding: chunked" headers,

then this is the described above case.

Since it is totally server issue, the solution should be to calculate Content-Length and to put it to response header on server side, if it is possible (or to use Chunked transfer encoding).

Recommendation to close connections on the client side should be considered just as a workaround, keep in mind that it degrades overall performance.

TiMoFeJ
  • 111
  • 1
  • 3
  • 3
    This sounded really promising, but I checked my headers and I have `Connection: keep-alive` and `Transfer-Encoding: chunked`. I'm still having this issue. – Cullub Mar 19 '20 at 08:33
  • you cant observe socket connection, nowadays, android studio network profiling only support okhttp and httpurlconnection – Setmax Jan 19 '21 at 12:10
3

Just found out the solution
It is really a server side problem AND The solution is to send the content-length header
If you are using php just make your code like this

<?php
ob_start();
// the code - functions .. etc ... example:
$v = print_r($_POST,1);
$v .= "\n\r".print_r($_SERVER,1);
$file = 'file.txt';
file_put_contents($file,$v);
print $v;


// finally
$c = ob_get_contents();
$length = strlen($c);
header('Content-Length: '.$length);
header("Content-Type: text/plain");
//ob_end_flush(); // DID NOT WORK !!
ob_flush()
?>

The trick used here is to send the content-length header using the output buffer

Alaa Morad
  • 445
  • 4
  • 13
3

I had the same problem, turned out I still had the proxy configured on the emulator while didn't have Charles open anymore

Rule
  • 629
  • 1
  • 9
  • 19
1

If you're using OkHttp with the Ktor client (eg with kotlin multiplatform), consider enabling OkHttp's retryOnConnectionFailure configuration parameter (it's likely enabled by default in other environments).

As documented, this enables the client to silently recover from:

Stale pooled connections. The ConnectionPool reuses sockets to decrease request latency, but these connections will occasionally time out.

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*

HttpClient(OkHttp) {
    engine {
        config {
            retryOnConnectionFailure(true)
        }
    }
}

h/t

Nolan Amy
  • 10,785
  • 5
  • 34
  • 45
0

I was testing my App with localhost using XAMPP and this error was occurring, The problem was with the port i was using skype using 443 port i simply quit skype and error was resolved!

Tabish
  • 390
  • 3
  • 12
0

Its a server error. It means somehow execution returns to your client without the server sending actual response header.

If you have control over server code, check that after processing the request, you explicitly send a response header with a response code. That way retrofit knows the request has been processed.

katwekibs
  • 1,342
  • 14
  • 17
0

I have the same issue. This error is caused by the server-side only supports http2. You need to update JDK to a version that supports http2 (>=jdk9) to fix this issue.

Viet
  • 443
  • 1
  • 5
  • 16
0

Add "Connection: keep-alive" to yor Rest Api endpoint

 @Headers({"Content-Type: application/json", "Accept: application/json", "Connection: keep-alive"})

This is if your endpoint is being called consecutively

-1

This may be an old thread, as for me, check your internet connection (Wifi) there might be some restriction on accessing some of your endpoints.

I've fixed mine by using/connecting to my mobile data.

-cheers/happy codings

ralphgabb
  • 10,298
  • 3
  • 47
  • 56
-1

Most probably there are 2 things, happening at the same time.
First, the url contains a port which is not commonly used AND secondly, you are using a VPN or proxy that does not support that port.
Personally, I had the same problem. My server port was 45860 and I was using pSiphon anti-filter VPN.
In that condition my Postman reported "connection hang-up" only when server's relpy was an error with status codes bigger than 0. (it was fine when some text was returning from server with no error code)
Then I changed my web service port to 8080 on my server and, WOW, it worked! although psiphon vpn was connected.
Therefore, my suggestion is, if you can change the server port, so try it, or check if there is a proxy problem