如何使用HTTPClient的HEAD方法获取所有标头

发布于 2024-12-10 17:48:33 字数 639 浏览 0 评论 0原文

我必须使用 HttpClientHEAD 方法来获取标头字段并检查服务器文件的“上次修改”日期。
我无法得到这一点,如果您知道如何获取标题字段,请回复。 如何将“last-modified”标头获取到 String 对象中进行比较。

这是我的代码:

HttpClient client = new DefaultHttpClient();
//HttpGet get = new HttpGet(url);
HttpHead method = new HttpHead(url);
HttpResponse response= client.execute(method);

Header[] s = response.getAllHeaders();

System.out.println("THe header from the httpclient:");
for(int i=0; i < s.length; i++){
    Header hd = s[i];
    System.out.println("Header Name: "+hd.getName()
                        +"       "+" Header Value: "+ hd.getValue());
}

I have to use HEAD method of HttpClient to get the header field and to check the "last-modified" date of server file.
I am not able to get that, if you know how to get the header field then please reply.
How to get the "last-modified" header into the String object for the comparison.

Here is my code:

HttpClient client = new DefaultHttpClient();
//HttpGet get = new HttpGet(url);
HttpHead method = new HttpHead(url);
HttpResponse response= client.execute(method);

Header[] s = response.getAllHeaders();

System.out.println("THe header from the httpclient:");
for(int i=0; i < s.length; i++){
    Header hd = s[i];
    System.out.println("Header Name: "+hd.getName()
                        +"       "+" Header Value: "+ hd.getValue());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…红帽 2024-12-17 17:48:34

在 httpClient 4.5 上您将使用:

final HttpHead headMethod = new HttpHead(fileUri);
final Header header = headMethod.getFirstHeader("last-modified");
final String lastModified = header.getValue();

On httpClient 4.5 you would use:

final HttpHead headMethod = new HttpHead(fileUri);
final Header header = headMethod.getFirstHeader("last-modified");
final String lastModified = header.getValue();
一人独醉 2024-12-17 17:48:34

从 HttpClient 文档

HeadMethod head = new HeadMethod("http://jakarta.apache.org");

// Excecute the method here with your HttpClient

Header[] headers = head.getResponseHeaders();
String lastModified = head.getResponseHeader("last-modified").getValue();

您需要添加自己的错误处理。

From the HttpClient documentation

HeadMethod head = new HeadMethod("http://jakarta.apache.org");

// Excecute the method here with your HttpClient

Header[] headers = head.getResponseHeaders();
String lastModified = head.getResponseHeader("last-modified").getValue();

You'll need to add your own error handling.

℡Ms空城旧梦 2024-12-17 17:48:34

最好使用这样的东西:

CloseableHttpClient client = HttpClientBuilder.create().build();
HttpHead head = new HttpHead(url);
String lastModified;
try {
    CloseableHttpResponse response = client.execute(head);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 200) {
        Header header = headMethod.getFirstHeader("last-modified");
        lastModified = header.getValue();
    }
} catch (IOException ignored) {
}

It would be best to use something like this:

CloseableHttpClient client = HttpClientBuilder.create().build();
HttpHead head = new HttpHead(url);
String lastModified;
try {
    CloseableHttpResponse response = client.execute(head);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 200) {
        Header header = headMethod.getFirstHeader("last-modified");
        lastModified = header.getValue();
    }
} catch (IOException ignored) {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文