Java – Android EOFException appears when using the HttpURLConnection header

Android EOFException appears when using the HttpURLConnection header… here is a solution to the problem.

Android EOFException appears when using the HttpURLConnection header

I signed my http request using a custom authorization header:

String key="client="+USER+",hash="+sha1(STR, API_KEY)+",timestamp="+t.toString();

If anyone is interested in the SHA1 method: http://pastebin.com/HRFXQ4Bk the key string is used as header:

URL url = new URL(sb.toString());

HttpURLConnection conn = null;  
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", key);
conn.setRequestMethod("GET");

InputStreamReader in = new InputStreamReader(conn.getInputStream());

I get

the following error when I try to get a response:

10-28 18:25:40.111: E/error(6855): java.io.EOFException 10-28
18:25:40.111: E/error(6855): at
libcore.io.Streams.readAsciiLine(Streams.java:203) 10-28 18:25:40.111:
E/error(6855): at
libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
10-28 18:25:40.111: E/error(6855): at
libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827) 10-28
18:25:40.111: E/error(6855): at
libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
10-28 18:25:40.111: E/error(6855): at
libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)

On my server, no access is logged for this request. However, when I delete the auth header, a connection to my server is established based on the server logs.

So how does the auth header affect requests? Why is there no connection using headers?


By the way, titles like this

conn.setRequestProperty("Authorization", "FOOBAR");

valid, but denied because the authorization header does not meet the requirements:

10-29 08:12:07.235: E/error(23663): java.net.ConnectException: failed
to connect to api.myserver.net (port 1337):
connect failed: ECONNREFUSED (Connection refused) 10-29 08:12:07.235:
E/error(23663): at libcore.io.IoBridge.connect(IoBridge.java:114)
10-29 08:12:07.235: E/error(23663): at
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 10-29
08:12:07.235: E/error(23663): at
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)

My network service requires headers to have the following format

match(/client=([^,]*),hash=([^,]*),timestamp=([^,]*)/);

So this exception is not the same as the original exception. When I remove the header and disable authorization on my web service, the connection works as expected. So the problem seems to be with the custom authorization header. Any ideas?

Solution

Based on this question It seems that this may be an issue with some Android versions

Try adding conn.setRequestProperty("Connection", "close") to your request.

Related Problems and Solutions