0

I'm trying to check if Internet connection is available on android devices but my internet connection requires a proxy to reach internet.

This is my code:

URL myurl = new URL("http://www.google.com");
HttpURLConnection connection;
InetSocketAddress proxyInet = new InetSocketAddress(session.getUrlProxy(),session.getPortProxy());
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyInet);
connection = (HttpURLConnection)myurl.openConnection(proxy);
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
int responseCode = -1;
responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                      connected= true;
                      connection.disconnect();
                }
                else {
                    connection.disconnect();
                }

This code returns this exception message:

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
04-28 09:33:38.115 25512-25672/com.asde.minimental W/System.err:     at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
04-28 09:33:38.119 25512-25672/com.asde.minimental W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
04-28 09:33:38.122 25512-25672/com.asde.minimental W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
04-28 09:33:38.122 25512-25672/com.asde.minimental W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
04-28 09:33:38.125 25512-25672/com.asde.minimental W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
04-28 09:33:38.129 25512-25672/com.asde.minimental W/System.err:     at java.io.InputStream.read(InputStream.java:163)
04-28 09:33:38.135 25512-25672/com.asde.minimental W/System.err:     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
04-28 09:33:38.139 25512-25672/com.asde.minimental W/System.err:     at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
04-28 09:33:38.142 25512-25672/com.asde.minimental W/System.err:     at libcore.io.Streams.readAsciiLine(Streams.java:201)
04-28 09:33:38.145 25512-25672/com.asde.minimental W/System.err:     at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
04-28 09:33:38.149 25512-25672/com.asde.minimental W/System.err:     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
04-28 09:33:38.149 25512-25672/com.asde.minimental W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
04-28 09:33:38.149 25512-25672/com.asde.minimental W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
04-28 09:33:38.152 25512-25672/com.asde.minimental W/System.err:     at com.asde.minimental.utilities.Utils.isInternetConn(Utils.java:283)
04-28 09:33:38.152 25512-25672/com.asde.minimental W/System.err:     at com.asde.minimental.net.AsyncUpdater.doInBackground(AsyncUpdater.java:58)
04-28 09:33:38.155 25512-25672/com.asde.minimental W/System.err:     at com.asde.minimental.net.AsyncUpdater.doInBackground(AsyncUpdater.java:30)
04-28 09:33:38.155 25512-25672/com.asde.minimental W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-28 09:33:38.159 25512-25672/com.asde.minimental W/System.err:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-28 09:33:38.162 25512-25672/com.asde.minimental W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-28 09:33:38.165 25512-25672/com.asde.minimental W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-28 09:33:38.165 25512-25672/com.asde.minimental W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-28 09:33:38.169 25512-25672/com.asde.minimental W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-28 09:33:38.175 25512-25672/com.asde.minimental W/System.err:     at java.lang.Thread.run(Thread.java:856)
04-28 09:33:38.179 25512-25672/com.asde.minimental W/System.err: Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
04-28 09:33:38.182 25512-25672/com.asde.minimental W/System.err:     at libcore.io.Posix.recvfromBytes(Native Method)
04-28 09:33:38.185 25512-25672/com.asde.minimental W/System.err:     at libcore.io.Posix.recvfrom(Posix.java:132)
04-28 09:33:38.185 25512-25672/com.asde.minimental W/System.err:     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
04-28 09:33:38.185 25512-25672/com.asde.minimental W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
04-28 09:33:38.185 25512-25672/com.asde.minimental W/System.err:    ... 21 more

This code without proxy runs fine

connection = (HttpURLConnection)myurl.openConnection();

What would be wrong?

Thanks

EDIT1: If I try with https://stackoverflow.com/ it works correctly. The problem seems to be with the google server which is reaching the request

Community
  • 1
  • 1
Hanzo
  • 1,839
  • 4
  • 30
  • 51

1 Answers1

0

If you don't put this permission, write it on the top of the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

After (or if it's already put), set this property before connecting for your HttpURLConnection

connection.setRequestProperty("connection", "close");
Maitre Manuel
  • 21
  • 1
  • 5
  • 1
    Before connection = (HttpURLConnection)myurl.openConnection(proxy); ? – Hanzo Apr 28 '16 at 08:14
  • Yes sir! If it don't work, read this post, a tutorial to use java.net.URLConnection and Http Request http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests – Maitre Manuel Apr 28 '16 at 08:21
  • I have added `connection.setRequestProperty("connection", "close");` before `connection.connect()` and the problem is the same. The exception is launched by `connection.getResponseCode();`. When I try this method to do a `POST` request to my own server (own web) it works. Any idea? – Hanzo Apr 28 '16 at 08:54
  • I saw in an other question maybe a problem with your network. Have you got the same problem with a Wifi connection or a 3G/4G ? 3G/4G block it (Access forbidden) – Maitre Manuel Apr 28 '16 at 09:08
  • I have tried through wifi (Android tablet). The problem seems related with the type of the request to the google server. If i try to do a POST request with this method directly to my server it works – Hanzo Apr 28 '16 at 09:13
  • Hm ok so your code is correct, it's a problem between your device and the Google server. It possible Google think your request is a DDOS attack and block it. Check your firewall or if applications are between your device and the Google serve and block your request with their own firewall. If it's that i can't help anymore, sorry :/ ... – Maitre Manuel Apr 28 '16 at 09:37
  • I've tried with http://stackoverflow.com/ and my server and it works with my code. I don't know which is the problem :( – Hanzo Apr 28 '16 at 09:39
  • If you make a `POST` it (post-it haha joke) work between [stackoverflow.com](http://stackoverflow.com) and your serve it work but not with google ? (I need to be sure and it's the end of the half-day work, I need my lunch and my coffee else my mind-comprehension is broken :p) – Maitre Manuel Apr 28 '16 at 09:50
  • Sorry. If I do a normal http request, like the explained in my original code, with the url http://www.stackoverflow.com instead of http://www.google.com it works. Not POST. Thanks – Hanzo Apr 28 '16 at 10:22