2011-06-15 76 views
0

我想查一個網址的存在,我試圖在Java下面的代碼,檢查網址是否存在,而無需打開瀏覽器,在Java

public boolean exists(String URLName) { 
    try { 
     if (!URLName.toUpperCase().contains("HTTP")) 
      URLName="http://"+URLName; 
      URL url = new URL(URLName); 
      System.setProperty("java.net.useSystemProxies", "true"); 
      HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
       urlConn.setConnectTimeout(9000); 
     urlConn.setReadTimeout(9000);    
      urlConn.connect();      
         if(HttpURLConnection.HTTP_OK == urlConn.getResponseCode()) 
      return true; 
else 
      return false; 
    } 
catch (SocketTimeoutException e){ 
     return false; 
    } 

catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

上面的代碼在Windows中工作正常,但是當代碼通過服務器(Linux版)我收到以下錯誤信息運行,

GConf Error: Failed to contact configuration server; some possible 
causes are that you need to enable TCP/IP networking for ORBit, or 
you have stale NFS locks due to a system crash. See 
http://projects.gnome.org/gconf/ for information. (Details - 1: 
Not running within active session) 
GConf Error: Failed to contact configuration server; some possible 
causes are that you need to enable TCP/IP networking for ORBit, or 
you have stale NFS locks due to a system crash. See 
http://projects.gnome.org/gconf/ for information. (Details - 1: 
Not running within active session) 
java.net.UnknownHostException: www.ep.gov 
at 
java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:177) 
at 
java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366) 
at java.net.Socket.connect(Socket.java:525) 
at sun.net.NetworkClient.doConnect(NetworkClient.java :158) 
at 
sun.net.http://www.http.HttpClient.openServe...lient.java:394) 
at 
sun.net.http://www.http.HttpClient.openServe...lient.java:529) 
at sun.net.www.http.HttpClient.<init>(HttpClient.java :233) 
at sun.net.www.http.HttpClient.New(HttpClient.java:306) 
at sun.net.www.http.HttpClient.New(HttpClient.java:323) 
at 
sun.net.http://www.protocol.http.HttpURLConn...ction.java:860) 
at 
sun.net.http://www.protocol.http.HttpURLConn...ction.java:801) 
at 
sun.net.http://www.protocol.http.HttpURLConn...ction.java:726) 

我從服務器管理下面的消息

It is working, while it was run in X session (GUI Mode). 
But it is not working in non-GUI mode. Please try to change the coding to make use of non-GUI mode." It seems the above code is making use of GUI mode. 

我需要一個替代代碼來檢查網址的存在,而無需使用GUI。

問候 琳達

回答

2

UnknownHostException意味着該請求不到達其目的地。可能有很多原因導致linux服務器無法訪問所需的URL www.ep.gov

請確保服務器可以訪問URL www.ep.gov

1

我覺得你的問題是這一行:

System.setProperty("java.net.useSystemProxies", "true"); 

看來這會導致系統試圖找到一個Web瀏覽器的HTTP代理服務器配置,並最終運行到一些問題GNOME配置。

您是否確實需要使用HTTP代理?如果不是,只需刪除該行。如果你這樣做,請閱讀this page to learn how to configure proxies

相關問題