我正在使用以下代码来检查网站的可用性:

public boolean isSiteAvailable() {

    try {
        URL u = new URL("http://www.google.com");
        HttpURLConnection huc =  (HttpURLConnection) u.openConnection();
        huc.setRequestMethod("GET");
        huc.connect();
        int response=huc.getResponseCode();

        if ((response >= 400) || (response < 200))
            return false;

        return true;
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        System.out.println("OOPSIE");
        e.printStackTrace();
    }

    return false;
}


但是,我收到以下消息..

    09-28 19:45:41.487: WARN/System.err(28801): java.net.SocketException: The operation timed out
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
    09-28 19:45:41.487: WARN/System.err(28801):     at java.net.Socket.connect(Socket.java:1055)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
    09-28 19:45:41.487: WARN/System.err(28801):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
    09-28 19:45:41.487: WARN/System.err(28801):     at corp.mithila.app.mithila.isSiteAvailable(mithila1.java:210)
    09-28 19:45:41.487: WARN/System.err(28801):     at corp.mithila.app.mithila$5.run(mithila1.java:121)
    09-28 19:45:41.487: WARN/System.err(28801):     at android.os.Handler.handleCallback(Handler.java:587)
    09-28 19:45:41.487: WARN/System.err(28801):     at android.os.Handler.dispatchMessage(Handler.java:92)
    09-28 19:45:41.487: WARN/System.err(28801):     at android.os.Looper.loop(Looper.java:123)
    09-28 19:45:41.487: WARN/System.err(28801):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    09-28 19:45:41.487: WARN/System.err(28801):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-28 19:45:41.487: WARN/System.err(28801):     at java.lang.reflect.Method.invoke(Method.java:521)
    09-28 19:45:41.487: WARN/System.err(28801):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    09-28 19:45:41.487: WARN/System.err(28801):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    09-28 19:45:41.487: WARN/System.err(28801):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

在代码中添加了以下行:

 huc.setRequestProperty("Connection", "close");
 huc.setConnectTimeout(2000); // Timeout 2 seconds.


这似乎已经解决了问题。

10-07 19:14