【问题标题】:How to check if a URL exists or returns 404 with Java?如何检查 URL 是否存在或使用 Java 返回 404?
【发布时间】:2010-11-25 13:53:43
【问题描述】:
String urlString = "http://www.nbc.com/Heroes/novels/downloads/Heroes_novel_001.pdf";
URL url = new URL(urlString);
if(/* Url does not return 404 */) {
    System.out.println("exists");
} else {
    System.out.println("does not exists");
}
urlString = "http://www.nbc.com/Heroes/novels/downloads/Heroes_novel_190.pdf";
url = new URL(urlString);
if(/* Url does not return 404 */) {
    System.out.println("exists");
} else {
    System.out.println("does not exists");
}

这应该打印出来

exists
does not exists

测试

public static String URL = "http://www.nbc.com/Heroes/novels/downloads/";

public static int getResponseCode(String urlString) throws MalformedURLException, IOException {
    URL u = new URL(urlString); 
    HttpURLConnection huc =  (HttpURLConnection)  u.openConnection(); 
    huc.setRequestMethod("GET"); 
    huc.connect(); 
    return huc.getResponseCode();
}

System.out.println(getResponseCode(URL + "Heroes_novel_001.pdf")); 
System.out.println(getResponseCode(URL + "Heroes_novel_190.pdf"));   
System.out.println(getResponseCode("http://www.example.com")); 
System.out.println(getResponseCode("http://www.example.com/junk"));           

输出

200
200
200
404

解决方案

在 .connect() 之前添加下一行,输出将为 200、404、200、404

huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");

【问题讨论】:

  • 我在您的测试中没有发现问题。在我的浏览器中,我没有得到第二个结果的内容,但没有得到 404
  • 事实上,我似乎得到了一个大部分空白的 HTML 页面
  • 该网站似乎提供了几乎所有内容的有效内容。例如www.nbc.com/junk。试试example.com/junk.html
  • URL nbc.com/Heroes/novels/downloads/Heroes_novel_190.pdf 给了我一个完全空白的页面(甚至没有 标签),但带有 404 标头。对用户不是很好,但在技术上是正确的。
  • 你应该把解决方案分成一个答案,这样我也可以投票!

标签: java url http-status-code-404


【解决方案1】:

你可能想添加

HttpURLConnection.setFollowRedirects(false);
// note : or
//        huc.setInstanceFollowRedirects(false)

如果你不想跟随重定向 (3XX)

您只需要一个“HEAD”而不是“GET”。

huc.setRequestMethod("HEAD");
return (huc.getResponseCode() == HttpURLConnection.HTTP_OK);

【讨论】:

  • HEAD +1,人们不时忘记 HTTP 是如何工作的,还好有些人还记得 :)
  • 处理 HTTPS 网址更棘手,对吧?必须管理证书...
【解决方案2】:

这对我有用:

URL u = new URL ( "http://www.example.com/");
HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
huc.setRequestMethod ("GET");  //OR  huc.setRequestMethod ("HEAD"); 
huc.connect () ; 
int code = huc.getResponseCode() ;
System.out.println(code);

感谢以上建议。

【讨论】:

    【解决方案3】:

    通过在您的 URL 对象上调用 openConnection() 来使用 HttpUrlConnection

    getResponseCode() 将在您从连接中读取数据后为您提供 HTTP 响应。

    例如

       URL u = new URL("http://www.example.com/"); 
       HttpURLConnection huc = (HttpURLConnection)u.openConnection(); 
       huc.setRequestMethod("GET"); 
       huc.connect() ; 
       OutputStream os = huc.getOutputStream(); 
       int code = huc.getResponseCode(); 
    

    (未测试)

    【讨论】:

      【解决方案4】:

      您的代码没有任何问题。这是 NBC.com 对你耍花招。当 NBC.com 确定您的浏览器无法显示 PDF 时,它只会返回一个网页,而不管您请求什么,即使它不存在。

      你需要通过告诉它你的浏览器有能力来欺骗它,比如,

      conn.setRequestProperty("User-Agent",
          "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.13) Gecko/2009073021 Firefox/3.0.13");
      

      【讨论】:

        【解决方案5】:

        根据问题中给出的答案和信息,这是您应该使用的代码:

        public static boolean doesURLExist(URL url) throws IOException
        {
            // We want to check the current URL
            HttpURLConnection.setFollowRedirects(false);
        
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        
            // We don't need to get data
            httpURLConnection.setRequestMethod("HEAD");
        
            // Some websites don't like programmatic access so pretend to be a browser
            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
            int responseCode = httpURLConnection.getResponseCode();
        
            // We only accept response code 200
            return responseCode == HttpURLConnection.HTTP_OK;
        }
        

        当然经过测试和工作。

        【讨论】:

          猜你喜欢
          • 2011-08-24
          • 2011-04-05
          • 2020-03-25
          • 1970-01-01
          • 2019-11-27
          • 2012-05-20
          • 1970-01-01
          • 2015-11-03
          • 1970-01-01
          相关资源
          最近更新 更多