如何使用HTTPClient的HEAD方法获取所有标头
我必须使用 HttpClient
的 HEAD
方法来获取标头字段并检查服务器文件的“上次修改”日期。
我无法得到这一点,如果您知道如何获取标题字段,请回复。 如何将“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 httpClient 4.5 上您将使用:
On httpClient 4.5 you would use:
从 HttpClient 文档
您需要添加自己的错误处理。
From the HttpClient documentation
You'll need to add your own error handling.
最好使用这样的东西:
It would be best to use something like this: