检查是否可通过代理连接Internet连接

时间:2016-04-28 07:52:02

标签: java android proxy httpurlconnection

我试图检查Android设备上是否有互联网连接,但我的互联网连接需要代理才能上网。

这是我的代码:

let sema = dispatch_semaphore_create(2); //depending how many downloads you want to go at once
for i in 0..<self.resultArr.count {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

        //download images here, order of execution will not be guaranteed, but after they are finished, they will always put the images in the array at 'i' so it doesnt matter

        dispatch_semaphore_signal(sema);
    })
}

此代码返回此异常消息:

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();
                }

此代码没有代理运行正常

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

会出现什么问题?

由于

EDIT1:如果我尝试使用https://stackoverflow.com/,它可以正常工作。问题似乎是谷歌服务器正在达到请求

1 个答案:

答案 0 :(得分:0)

如果您未授予此权限,请将其写在AndroidManifest.xml的顶部

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

之后(或者如果它已经放好),在连接HttpURLConnection之前设置此属性

connection.setRequestProperty("connection", "close");