java – HTTP Get:只下载头文件? (HEAD不支持)

前端之家收集整理的这篇文章主要介绍了java – HTTP Get:只下载头文件? (HEAD不支持)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的代码中,我使用一些Http Get请求来将一些文件作为流来下载.我使用以下代码
public String getClassName(String url) throws ClientProtocolException,IOException {
        HttpResponse response = sendGetRequestJsonText(url);

        Header[] all = response.getAllHeaders();
        for (Header h : all) {
            System.out.println(h.getName() + ": " + h.getValue());
        }

        Header[] headers = response.getHeaders("Content-Disposition");
        InputStreamParser.convertStreamToString(response.getEntity().getContent());
        String result = "";
        for (Header header : headers) {
            result = header.getValue();
        }
        return result.substring(result.indexOf("''") + "''".length(),result.length()).trim();
    }

但是这会下载响应的全部内容.我想仅检索没有内容的HTTP标头. HEAD请求似乎不起作用,因为我得到状态501,没有实现.我怎样才能做到这一点?

解决方法

您可以考虑仅提出一个 HEAD请求,而不是发出GET请求:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The Metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining Metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity,accessibility,and recent modification.

原文链接:https://www.f2er.com/java/125927.html

猜你在找的Java相关文章