如何实现httpclient的HEAD方法

发布于 2024-12-10 22:12:38 字数 599 浏览 2 评论 0原文

您好,我正在从服务器下载文件。我必须使用 HEAD 方法获取元信息。 andybody 帮助我实现 HEAD 方法来获取“上次修改”日期和修改日期。

这是我的代码:

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());
}

//here I have to implement the HEAD method

Hi I am downloading file from server. I have to take meta-information using HEAD method. andybody help me to implement the HEAD method to get "last-modified" date and modified-since date.

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());
}

//here I have to implement the HEAD method

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

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

发布评论

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

评论(1

隐诗 2024-12-17 22:12:39

HEAD 和 GET 方法之间的区别在于响应不包含正文。否则,两者是相同的。换句话说,HEAD 方法获取所有标头。它用于获取单个标头的数据,它只是一次检索所有标头。

在代码示例中,您已经拥有所有标头,因为您执行了 HEAD 请求。在 for 循环中,您输出标头中的所有数据。如果 last-modified 不存在,则服务器没有为此资源提供它。

请注意,if-modified-since 是请求标头字段,而不是响应标头字段。您可以将其设置为指示服务器仅在修改日期已过时才返回资源。如果您打算仅在服务器上修改资源时检索该资源,则可以仅使用带有 if-modified-since 标头集的 GET 请求。要了解服务器是否支持此标头,请检查此工具: http://www.feedthebot.com /工具/如果修改/

The difference between a HEAD and a GET method is that the response will not contain a body. Otherwise, the two are the same. In other words, a HEAD method gets all the headers. It is not used for getting data of a single header, it just retrieves all headers at once.

In the code example you already have all headers, because you executed a HEAD request. In the for-loop you output all data from the headers. If the last-modified is not there, the server did not provide it for this resource.

Note that the if-modified-since is a request header field, not a response header field. You can set it to instruct the server to only return the resource if the modified-since date has passed. If you intend to only retrieve a resource when it has been modified on the server, you can just use a GET request with the if-modified-since header set. To know whether a server supports this header, check this tool: http://www.feedthebot.com/tools/if-modified/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文