gpt4 book ai didi

java - doGet 和 doHead 方法之间的区别

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:16 26 4
gpt4 key购买 nike

我知道 doHead 方法不会通过 http 发送正文,但是 doGet 也不会。两者之间有明显的区别吗?提前致谢。

最佳答案

简答

HEAD 只发回与相应 GET 请求相同的头信息,但没有实体主体。 GET 将发送相同的 header 和实体主体。当然,在 GET 不会在其主体中返回任何内容的罕见/有争议的情况下,GET 和 HEAD 的行为相似。

本书HTTP The Definitive Guide添加:

This allows a client to inspect the headers for a resource without having to actually get the resource. Using HEAD, you can:

  • Find out about a resource (e.g., determine its type) without getting it.
  • See if an object exists, by looking at the status code of the response.
  • Test if the resource has been modified, by looking at the headers.

Server developers must ensure that the headers returned are exactly those that a GET request would return. The HEAD method also is required for HTTP/1.1 compliance.

因此,doHead 是否调用 doGet 可能只是一个实现细节,但如果是这样,doHead 将需要剥离主体doGet 在发送响应之前。但是,此实现效率低下,因为理想情况下 doHead 不应导致执行整个 doGet 请求以确定要发回的 header 的困难。

不过,某些 header 的计算可能会造成困难。例如,“Content-Length” header 意味着我们需要知道实际资源的大小,因此 HEAD 可能需要“GET”资源以确定相应 GET 请求的实际大小(以字节为单位),但这不会造成负担网络实际发送回资源​​,尽管它可能会给服务器增加资源检索的负担,以确定要发送回哪些 header (即 Content-Length、Content-Type、Content-Language 等)

长答案

HTTP specification

9.4 HEAD

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.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

而对于 GET 它说:

9.3 GET

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.

The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.

The semantics of the GET method change to a "partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client.

The response to a GET request is cacheable if and only if it meets the requirements for HTTP caching described in section 13.

See section 15.1.3 for security considerations when used for forms.

如果你read the source code for javax.servlet.http.HttpServlet你可以看到它的文档说:

Receives an HTTP HEAD request from the protected service method and handles the request. The client sends a HEAD request when it wants to see only the headers of a response, such as Content-Type or Content-Length. The HTTP HEAD method counts the output bytes in the response to set the Content-Length header accurately. If you override this method, you can avoid computing the response body and just set the response headers directly to improve performance. Make sure that the doHead method you write is both safe and idempotent (that is, protects itself from being called multiple times for one HTTP HEAD request). If the HTTP HEAD request is incorrectly formatted, doHead returns an HTTP "Bad Request" message.

默认方法实现如下所示:

protected void doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
NoBodyResponse response = new NoBodyResponse(resp);
doGet(req, response);
response.setContentLength();
}

如果您阅读 NoBodyResponse 的代码,您会发现它只是一个响应,其输出流丢弃所有数据,并且仅计算字节数以确定相应 GET 响应的正确 Content-Length。

因此,这就是 Servlet 规范建议如何生成有效的 HEAD 响应的方式。只要您遵守 HTTP 规范准则,您就可以覆盖此行为以使其更高效。

关于java - doGet 和 doHead 方法之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966794/

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