【问题标题】:Check if URL is valid or exists in java检查 URL 是否有效或存在于 java 中
【发布时间】:2012-05-20 01:50:54
【问题描述】:

我正在编写一个 Java 程序,它会访问一个 url 列表,并且需要首先知道该 url 是否存在。我不知道该怎么做,也找不到要使用的java代码。

网址是这样的:

http: //ip:port/URI?Attribute=x&attribute2=y

这些是我们内部网络上的 URL,如果有效,它们将返回 XML。

谁能推荐一些代码?

【问题讨论】:

  • 如何记录一个restful api?

标签: java http url


【解决方案1】:

您可以使用httpURLConnection。如果它无效,您将不会得到任何回报。

    HttpURLConnection connection = null;
try{         
    URL myurl = new URL("http://www.myURL.com");        
    connection = (HttpURLConnection) myurl.openConnection(); 
    //Set request to header to reduce load as Subirkumarsao said.       
    connection.setRequestMethod("HEAD");         
    int code = connection.getResponseCode();        
    System.out.println("" + code); 
} catch {
//Handle invalid URL
}

或者您可以像从 CMD 中那样 ping 它并记录响应。

String myurl = "google.com"
String ping = "ping " + myurl 

try {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(ping);
    r.exec(ping);    
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String inLine;
    BufferedWriter write = new BufferedWriter(new FileWriter("C:\\myfile.txt"));

    while ((inLine = in.readLine()) != null) {             
            write.write(inLine);   
            write.newLine();
        }
            write.flush();
            write.close();
            in.close();              
     } catch (Exception ex) {
       //Code here for what you want to do with invalid URLs
     } 
}

【讨论】:

  • 第一种方法对我有用...非常感谢您的帮助..现在我在浏览器中打开链接的方式与在其他浏览器中打开链接的方式相同,只需直接提供完整链接.. ..再次感谢
  • 这对我也有帮助。
【解决方案2】:
  1. 格式错误的网址会给您一个例外。
  2. 要知道您的网址是否处于活动状态,您必须点击该网址。没有其他办法。

您可以通过从 url 请求一个 header 来减少负载。

【讨论】:

    【解决方案3】:
    package com.my;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.UnknownHostException;
    
    public class StrTest {
    
    public static void main(String[] args) throws IOException {
    
        try {
            URL url = new URL("http://www.yaoo.coi");
            InputStream i = null;
    
            try {
                i = url.openStream();
            } catch (UnknownHostException ex) {
                System.out.println("THIS URL IS NOT VALID");
            }
    
            if (i != null) {
                System.out.println("Its working");
            }
    
        } catch (MalformedURLException e) {
            e.printStackTrace();
               }
          }
      }
    

    输出:此 URL 无效

    【讨论】:

      【解决方案4】:

      打开一个连接并检查响应是否包含有效的 XML?是不是太明显了,还是你在寻找其他魔法?

      【讨论】:

        【解决方案5】:

        您可能想要使用 HttpURLConnection 并检查错误状态:

        HttpURLConnection javadoc

        【讨论】:

          猜你喜欢
          • 2014-07-24
          • 2010-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-13
          • 1970-01-01
          相关资源
          最近更新 更多