2013-11-27 97 views
1

當我測試url列表以查看連接是「活着」還是「死亡」時,我遇到了這個網站:(www.abbapregnancy.org),那麼網站是空白的,沒有任何信息。我怎樣才能做一個if語句來測試這樣的網站?隨意評論或建議的想法。我的代碼目前是這樣的:測試url連接java html

try 
        {// Test URL Connection 
        URL url = new URL("http://www."+line); 
        URLConnection conn = url.openConnection(); 
        conn.setDoOutput(true); 
        wr = new OutputStreamWriter(conn.getOutputStream()); 
        wr.flush(); 

        rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

        while(!rd.ready()) {} 

        if(rd.ready()) 
        { 
         //write to output file 
         System.out.println("Good URL: " + line); 
         urlAlive ++; 
         //increment alive line to Alive url file 
         aliveUrl += (line + System.getProperty("line.separator"));               
        } 
        else // increment dead url 
         urlDead++; 
        } 
        catch (Exception e) 
        // increment dead url 
         { 
          urlDead++; 
         } 
+0

從你的代碼看起來你試圖連接「轟TTP://www.www.abbapregnancy.org」。嘗試刪除「www」。從網站地址開始。 – TheSuccessor

+0

其實就是它的名單(abbapregnancy.org)。我自覺地加入(www)到上面的描述 –

回答

1

所以你想檢查的不僅僅是「活着」或「死亡」。如果還活着,你還想知道內容是否爲空,對嗎?

既然你似乎想檢查網站,使用http連接是有道理的。對於活着/死亡的情況,如果連接失敗,連接嘗試將引發異常。如果活着,您可以使用狀態代碼來檢查請求是否真的成功。最後,您可以使用content-length標題來檢查網站是否啓動,但實際返回空的內容。例如像這樣:

public void checkURL(URL url) throws IOException { 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setRequestMethod("GET"); 
    System.out.println(String.format("Fetching %s ...", url)); 
    try { 
     int responseCode = conn.getResponseCode(); 
     if (responseCode == 200) { 
      System.out.println(String.format("Site is up, content length = %s", conn.getHeaderField("content-length"))); 
     } else { 
      System.out.println(String.format("Site is up, but returns non-ok status = %d", responseCode)); 
     } 
    } catch (java.net.UnknownHostException e) { 
     System.out.println("Site is down"); 
    } 
} 
0
public static boolean IsReachable(Context context, String check_url) { 
    // First, check we have any sort of connectivity 
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final NetworkInfo netInfo = connMgr.getActiveNetworkInfo(); 
    boolean isReachable = false; 

    if (netInfo != null && netInfo.isConnected()) { 
     // Some sort of connection is open, check if server is reachable 
     try { 

      URL url = new URL(check_url); 

      // URL url = new URL("http://192.168.100.93/office_com/www/api.php/office_com/Logins/SignUp?api_user=endpoint"); 
      //URL url = new URL("http://10.0.2.2"); 
      HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); 
      urlc.setRequestProperty("User-Agent", "Android Application"); 
      urlc.setRequestProperty("Connection", "close"); 
      urlc.setConnectTimeout(10 * 1000); 
      try { 
       urlc.connect(); 
       System.out.println("-----fffff"); 
      } catch (Exception e) { 

       System.out.println("-----fffff " + e); 

      } 
      isReachable = (urlc.getResponseCode() == 200); 
     } catch (IOException e) { 

     } 
    } 

    return isReachable; 
} 
+0

這是一個git hub鏈接https://github.com/putulputul/CheckServerConnection – putulputul