2013-01-04 215 views
0

我正在使用HTTP後連接,並且我的應用程序定期連接到服務器。在某個點之後,我無法連接到服務器,並且我的所有連接都有這些例外情況:連接被服務器拒絕

java.net.ConnectException;未能連接到www.somedomain.com/xxx.xxx.xxx.x (80端口):連接失敗:ENETUNREACH(網絡無法訪問)

org.apache.http.conn.HttpHostConnectException;連接到http://www.somedomain.com拒絕

的java.net.UnknownHostException,無法解析主機「www.somedomain .com「:沒有地址與主機名關聯

我懷疑服務器可能會阻止我之後某一點。任何想法,爲什麼這可能會發生?重新安裝應用程序後,連接仍然被阻止。

編輯:這裏是發送HTTP代碼請求

public String send() throws ClientProtocolException, IOException { 
    InputStream is = null; 
    DefaultHttpClient httpclient = null; 
    HttpResponse response = null; 
    try { 
     System.setProperty("http.keepAlive", "false"); 
     httpclient = new DefaultHttpClient(); 
     HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false); 
     HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { 

      public boolean retryRequest(IOException exception, int executionCount, 
        HttpContext context) { 
       // retry a max of 5 times 
       if(executionCount >= 5){ 
        return false; 
       } 
       if(exception instanceof NoHttpResponseException){ 
        return true; 
       } else if (exception instanceof ClientProtocolException){ 
        return true; 
       } 
       return false; 
      } 
     }; 
     httpclient.setHttpRequestRetryHandler(retryHandler); 
     String proxyHost = android.net.Proxy.getDefaultHost(); 
     int proxyPort = android.net.Proxy.getDefaultPort(); 
     // Set Proxy params of client, if they are not the standard 
     if (proxyHost != null && proxyPort > 0) { 
      HttpHost proxy = new HttpHost(proxyHost, proxyPort); 
      httpclient.getParams().setParameter(
        ConnRoutePNames.DEFAULT_PROXY, proxy); 
     } 
     HttpPost httppost = new HttpPost(url); 
     httppost.setEntity(new UrlEncodedFormEntity(qData)); 
     response = httpclient.execute(httppost); 
     is = response.getEntity().getContent(); 
     return inputStreamToString(is); 
    } finally { 
     try { 
      if (is != null) { 
       is.close(); 
      } 
     } catch (Throwable e) { 
     } 
     try { 
      if (response != null && response.getEntity() != null) { 
       response.getEntity().consumeContent(); 
      } 
     } catch (Throwable e) { 
     } 
     try { 
      if (httpclient != null 
        && httpclient.getConnectionManager() != null) { 
       httpclient.getConnectionManager().shutdown(); 
      } 
     } catch (Throwable e) { 
     } 
    } 
} 
+0

提供您的代碼。你也開發服務器應用程序嗎? – sschrass

+0

@SatelliteSD我更新了帖子 –

+0

「無法解析主機」www.somedomain.com「:沒有與主機名關聯的地址」聽起來更像是一個DNS問題。 – kabuko

回答

1

您可以使用dig或nslookup來驗證DNS服務器。您可以在您的操作系統中配置一個已知的好DNS服務器,如8.8.8.8。 異常錯誤消息也可能會引起誤解:嘗試使用wireshark捕獲網絡流量,然後您將看到DNS查詢是否完全沒有返回,「未找到」,或者DNS是否正常,但服務器主機拒絕您的請求。

您還可以通過將www.somedomain.com添加到您的主機文件及其IP地址來繞過DNS不確定性。