简体   繁体   中英

Check if Internet connection is available through proxy

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

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM