Checking if a URL Exists in Java – 在Java中检查一个URL是否存在

最后修改: 2019年 7月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll be looking at how to check if a URL exists with an example in Java using the GET and HEAD HTTP methods.

在本教程中,我们将通过一个例子来了解如何使用GETHEAD HTTP方法在Java中检查一个URL是否存在。

2. URL Existence

2.URL的存在

There can be situations in programming when we have to know if a resource exists in the given URL before accessing it, or we may even need to check a URL to know the resource’s health.

在编程中,可能会有这样的情况:我们必须在访问某个资源之前知道它是否存在于给定的URL中,或者我们甚至可能需要检查一个URL以了解资源的健康状况。

We decide a resource’s existence at a URL by looking at its response code. Typically we look for a 200, which means “OK” and that the request has succeeded.

我们通过查看URL的响应代码来决定一个资源的存在。通常,我们寻找200,这意味着 “OK “和请求成功。

3. Using a GET Request

3.使用GET请求

First of all, to make a GET request, we can create an instance of java.net.URL and pass the URL that we would like to access as a constructor argument. After that, we simply open the connection and get the response code:

首先,要进行GET请求,我们可以创建一个java.net.URL的实例,并将我们想访问的URL作为构造参数传递给它。之后,我们只需打开连接并获得响应代码。

URL url = new URL("http://www.example.com");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
 
int responseCode = huc.getResponseCode();
 
Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);

When the resource is not found at the URL, we get a 404 response code:

当在URL中没有找到资源时,我们会得到一个404响应代码。

URL url = new URL("http://www.example.com/xyz"); 
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
 
int responseCode = huc.getResponseCode();
 
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);

As the default HTTP method in HttpURLConnection is GET, we’re not setting the request method in the examples in this section. We’ll see how to override the default method in the next section.

由于HttpURLConnection中的默认HTTP方法是GET,我们在本节的例子中没有设置请求方法。我们将在下一节中看到如何覆盖默认的方法。

4. Using a HEAD Request 

4.使用HEAD请求

The HEAD is also an HTTP request method that is identical to GET except that it does not return the response body. 

HEAD也是一种HTTP请求方法,除了不返回响应体外,它与GET相同。

It acquires the response code along with the response headers that we’ll receive if the same resource is requested with a GET method.

它获得了响应代码和响应头,如果用GET方法请求相同的资源,我们会收到这些响应头。

To create a HEAD request, we can simply set the Request Method to HEAD before getting the response code:

要创建一个HEAD请求,我们可以在获得响应代码之前简单地将请求方法设置为HEAD。

URL url = new URL("http://www.example.com");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("HEAD");
 
int responseCode = huc.getResponseCode();
 
Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);

Similarly, when the resource is not found at the URL:

同样地,当资源在URL中没有找到时。

URL url = new URL("http://www.example.com/xyz");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("HEAD");
 
int responseCode = huc.getResponseCode();
 
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);

By using the HEAD method and thereby not downloading the response body, we reduce the response time and bandwidth, and we improve performance.

通过使用HEAD方法,从而不下载响应体,我们减少了响应时间和带宽,并提高了性能

Although most modern servers support the HEAD method, some home-grown or legacy servers might reject the HEAD method with an invalid method type error. So, we should use the HEAD method with caution.

尽管大多数现代服务器都支持HEAD方法,一些自制的或传统的服务器可能会拒绝HEAD方法,出现无效的方法类型错误。所以,我们应该谨慎使用HEAD方法。

5. Following Redirects

5.跟踪重定向

Finally, when looking for URL existence, it might be a good idea not to follow redirects. But this can also depend on the reason we’re looking for the URL.

最后,在寻找URL存在时,不跟随重定向可能是一个好主意。但这也可能取决于我们寻找URL的原因。

When a URL is moved, the server can redirect the request to a new URL with 3xx response codes. The default is to follow a redirect. We can choose to follow or ignore the redirect based on our need.

当一个URL被移动时,服务器可以用3xx响应代码将请求重定向到一个新的URL。默认是跟随重定向。我们可以根据自己的需要,选择跟随或忽略重定向。

To do this, we can either override the default value of followRedirects for all the HttpURLConnections:

要做到这一点,我们可以覆盖所有HttpURLConnections的followRedirects默认值。

URL url = new URL("http://www.example.com");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
 
int responseCode = huc.getResponseCode();
 
Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);

Or, we can disable following redirects for a single connection by using the setInstanceFollowRedirects() method:

或者,我们可以通过使用setInstanceFollowRedirects()方法禁用单个连接的跟随重定向。

URL url = new URL("http://www.example.com");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setInstanceFollowRedirects(false);
 
int responseCode = huc.getResponseCode();
 
Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);

6. Conclusion

6.结论

In this article, we looked at checking the response code to find the availability of a URL. Also, we looked at how it might be a good idea to use the HEAD method to save bandwidth and get a quicker response.

在这篇文章中,我们研究了检查响应代码以发现一个URL的可用性。此外,我们还研究了使用HEAD方法来节省带宽并获得更快的响应可能是个好主意。

The code example used in this tutorial is available in our GitHub project.

本教程中使用的代码示例可在我们的GitHub项目中找到。