在java中无需打开浏览器即可检查url是否存在

发布于 2024-11-16 00:52:27 字数 2596 浏览 0 评论 0原文

我想检查 url 是否存在,我在 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. 

我需要一个备用代码来检查 url 是否存在而不使用 GUI。

问候 琳达

I want to check the existence of a url and i tried with the following code in 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;
   }
}

The above code is working fine in windows but when the code is run through server (linux) i am getting the following error message,

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)

I got the below message from server admin

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. 

I need an alternate code to check url existence without using GUI.

Regards
Linda

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2024-11-23 00:52:27

UnknownHostException 表示请求未到达其目的地。导致 Linux 服务器无法访问所需 URL www.ep.gov 的原因可能有很多。

请确保服务器可以访问 URL www.ep.gov

UnknownHostException means that the request is not reaching to its destination. There could be many reason that the linux server is unreachable to the desired URL www.ep.gov.

Please make sure that the server can reach the URL www.ep.gov.

如梦初醒的夏天 2024-11-23 00:52:27

我认为你的问题出在这一行:

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

这似乎会导致系统尝试查找 Web 浏览器的 HTTP 代理配置,并最终遇到 Gnome 配置的一些问题。

您真的需要使用 HTTP 代理吗?如果没有,只需删除该行即可。如果您这样做,请阅读此页面了解如何配置代理

I think your problem is with this line:

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

It seems this causes the system to try and find the HTTP proxy configuration for the web browser, and ends up running into some problems with the Gnome configuration.

Do you actually need to use a HTTP proxy? If not, simply remove that line. If you do, read this page to learn how to configure proxies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文