如何检查URL是否存在或使用Java返回404?

Ser*_*Amo 72 java url http-status-code-404

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");
}
Run Code Online (Sandbox Code Playgroud)

这应该打印

exists
does not exists
Run Code Online (Sandbox Code Playgroud)

测试

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"));           
Run Code Online (Sandbox Code Playgroud)

产量

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)");
Run Code Online (Sandbox Code Playgroud)

Rea*_*wTo 57

您可能想要添加

HttpURLConnection.setFollowRedirects(false);
// note : or
//        huc.setInstanceFollowRedirects(false)
Run Code Online (Sandbox Code Playgroud)

如果你不想遵循重定向(3XX)

不是做"GET",而是"HEAD"就是你所需要的.

huc.setRequestMethod("HEAD");
return (huc.getResponseCode() == HttpURLConnection.HTTP_OK);
Run Code Online (Sandbox Code Playgroud)

  • 对于HEAD +1,人们忘记了HTTP如何不时地工作,有些人还记得很好:) (15认同)
  • 处理 HTTPS url 更棘手,对吧?必须管理证书... (2认同)

小智 40

这对我有用:

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);
Run Code Online (Sandbox Code Playgroud)

感谢上面的建议.


Bri*_*new 23

通过调用URL对象来使用HttpUrlConnectionopenConnection().

从连接中读取后,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(); 
Run Code Online (Sandbox Code Playgroud)

(未测试)


ZZ *_*der 12

您的代码没有任何问题.这是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");
Run Code Online (Sandbox Code Playgroud)


Bul*_*aza 8

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

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;
}
Run Code Online (Sandbox Code Playgroud)

当然经过测试和工作.