1

I have this piece of code:

public static boolean checkIfURLExists(String targetUrl) {
        HttpURLConnection httpUrlConn;
        System.setProperty("http.keepAlive", "false");
        try {
            httpUrlConn = (HttpURLConnection) new URL(targetUrl)
                    .openConnection();
            httpUrlConn.setRequestMethod("HEAD");

            // Set timeouts in milliseconds
            httpUrlConn.setConnectTimeout(500);
            httpUrlConn.setReadTimeout(1000);

            // Print HTTP status code/message for your information.
            return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Removing the URL: " + targetUrl);
            return false;
        }
    }

wich tests if an URL is reachable. I call this piece of code multiple times with different URL's. However, the code gives me a EOFException on this line:

return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);

Exception:

12-18 11:11:57.655: W/System.err(30198): java.io.EOFException
12-18 11:11:57.655: W/System.err(30198):    at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
12-18 11:11:57.655: W/System.err(30198):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
12-18 11:11:57.665: W/System.err(30198):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:528)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:836)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
12-18 11:11:57.665: W/System.err(30198):    at com.cofely.gdfsuez.XmlDataParseHelper.checkIfURLExists(XmlDataParseHelper.java:172)

etc. Does anyone know what is going on, and how to fix this? Thanks

Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • Your timeouts are far too short. Why do you need to test if a URL is reachable? – user207421 Dec 18 '13 at 10:40
  • It's a OpenVPN app. And some users have more privileges than others. Like reaching some servers. So I don't want to give the users a link that won't ever work. But I found the solution already – Black Magic Dec 18 '13 at 10:47

1 Answers1

8

Oh, I just found my answer, appearently this is a bug in newer versions of android. Adding this line works for me:

httpUrlConn.setRequestProperty( "Accept-Encoding", "" ); 

Thanks

Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • Thanks a lot, this was driving me mad. – Wildcopper Jan 08 '15 at 14:06
  • Doesn't work! I tried 3 times. -1 – nick Jul 27 '16 at 10:14
  • 1
    @Lesperanza Well, at the time of this post the newer version was 4.0 I think, so logically it isn't the same on 5.0 and 6.0. Aside from that there are multiple reasons for the above posted error code. Just because mine was solved by this does not mean yours will too. It propably has a different reason. You see, there is no need to -1 my answer. – Black Magic Jul 27 '16 at 10:25
  • I have 4.4.2 Android version. What solution is now? – nick Jul 27 '16 at 10:27
  • I sadly don't know every solution to this problem. You have to keep looking or perhaps ask a question on StackOverflow yourself – Black Magic Jul 27 '16 at 14:32