URL:仅获取最后修改时间(无内容)

URL: fetch Last Modified only (without content)

有没有办法使用 HttpConnection 只获取 Last Modified header?

当我使用这段代码时:

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
long lastModified = httpCon.getLastModified();

在网络服务器的日志文件中,我看到:

a.b.c.d - - [26/Dec/2015:10:25:50 +0100] "GET /file.txt HTTP/1.1" 200 484 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"

如果我理解正确,GET 请求也会获取全部内容。是否有可能执行 HEAD 请求以仅检索 header?

您可以设置HTTP方法,见HttpURLConnection:

Set the method for the URL request, one of:

  • GET
  • POST
  • HEAD
  • OPTIONS
  • PUT
  • DELETE
  • TRACE

are legal, subject to protocol restrictions. The default method is GET.

示例:

HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setRequestMethod("HEAD");
long lastModified = httpUrlConnection.getLastModified();