【问题标题】:How to implement the HEAD method of httpclienthttpclient的HEAD方法如何实现
【发布时间】:2011-10-19 13:56:00
【问题描述】:

您好,我正在从服务器下载文件。我必须使用 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

【问题讨论】:

  • 在您提供给我们的代码示例中,您已经执行了一个完整的 HEAD。这包括所有标题信息。在那之后你为什么还要做一个额外的 HEAD 呢?似乎是多余的。 last-modified/modified-since 已经存在,如果服务器提供它们
  • 哦,这是什么语言?我假设 C#,但这些类似乎是 Apache 类,所以也许是 Java?请更新。

标签: methods httpclient head


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-12-10
    • 2015-10-21
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多