36

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?

8
  • 3
    You can't. It's a server error. Commented Aug 23, 2017 at 11:58
  • Is there any alternative solution? Commented Aug 23, 2017 at 12:00
  • Try using OkHttp. It worked for me some times, but i don't guarantee. Commented Aug 23, 2017 at 12:02
  • @phaneendratatapudi how to solve this error ? Commented Mar 14, 2018 at 6:48
  • what if this occurs with localhost, I am using xampp and getting same error but when i use Postman it works just fine .. Android connection problem? Commented Mar 26, 2018 at 6:26

14 Answers 14

48

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")
                             ...
Sign up to request clarification or add additional context in comments.

8 Comments

This is the answer. It works perfectly to me!!. I had been looking for hours.
I had a method with Request object passed as a parameter. Have to use the following code snippet instead >> request.addHeader("Connection", "close");
It's perfect answer!
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)
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!
|
12

"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.

2 Comments

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.
you cant observe socket connection, nowadays, android studio network profiling only support okhttp and httpurlconnection
11

I encountered this problem today. And it turns out that it is the server fault, as in the server threw 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

1 Comment

the server "threw"
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

2 Comments

It's a server side problem all right, but there's no evidence here that the server uses PHP.
I see what you mean, sorry for any misunderstanding, i'll edit my answer
3

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

Comments

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

2 Comments

it seems to be on by default
@bompf thanks – it seems Ktor is what was causing it to be disabled; answer updated.
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!

Comments

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.

Comments

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.

Comments

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

Comments

0

When I received the error "unexpected end of stream..." of Web3/Blockchain Node (Java, Spring Boot, Red Hat OpenShift, Hyperledger BESU):

@Component
public class BlockchainNetwork {

    @Value("${app.besu-network.host}")
    private String besuNetworkHost;

    public Web3j buildBesuNetwork() {
        return Web3j.build(new HttpService(besuNetworkHost));
    }
}

Both ways worked for me, with the same performance (response time), but the Singleton solution is better (because a best practice - pattern)!

Solution with Header "Connection:close":

@Component
public class BlockchainNetwork {

    @Value("${app.besu-network.host}")
    private String besuNetworkHost;

    public Web3j buildBesuNetwork() {
        HttpService httpService = new HttpService(besuNetworkHost);
        httpService.addHeader("Connection", "close");

        return Web3j.build(httpService);
    }
}

Solution with Singleton (pattern):

@Component
public final class BlockchainNetwork {

    @Value("${app.besu-network.host}")
    private String besuNetworkHost;

    private static Web3j web3jSingletonInstance;

    public Web3j buildBesuNetwork() {
        if (web3jSingletonInstance == null) {
            web3jSingletonInstance = Web3j.build(new HttpService(besuNetworkHost));
        }
        return web3jSingletonInstance;
    }
}

Comments

0

In my case I had do add Connection: Close header before sending request to backend service:

val targetHeaders = HttpHeaders().apply {
                    put("Connection", mutableListOf("Close"))
                }

Comments

-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

Comments

-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.