gpt4 book ai didi

java - HTTP Get : Only download the header?(不支持HEAD)

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:05:55 25 4
gpt4 key购买 nike

在我的代码中,我使用一些 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 header 。 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.

关于java - HTTP Get : Only download the header?(不支持HEAD),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8852392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com