简体   繁体   English

如何在不获取主体的情况下仅使用HTTP GET请求获取标头

[英]How to get only headers with HTTP GET request without fetching the body

Is there an opportunity to make http get request, read headers and not fetching body in order to reduce traffic and increase speed? 是否有机会使http获得请求,读取标头而不获取主体以减少流量并提高速度? I use this code: 我使用以下代码:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(u1);
        request.AllowAutoRedirect = true;
        request.Timeout = 30000;
        request.Method = "GET";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36";
        request.KeepAlive = true;
        HttpWebResponse response;

        using (response = (HttpWebResponse)request.GetResponse())
        {
            var res = response.ResponseUri.ToString();
        }

So I need only the result url. 所以我只需要结果网址。 If there is a redirection (Location header) - I will get the result url. 如果存在重定向(位置标头)-我将获得结果网址。 But I don't need the body. 但是我不需要身体。 Is it real to get response with GET method (not HEAD method) to get the result without the response body? 使用GET方法(而不是HEAD方法)获取响应以在没有响应主体的情况下获取结果是否真实?

Using GET on a resource that returns a body implies that you want to get the body. 在返回正文的资源上使用GET表示您要获取正文。 HEAD is designed exactly for what you're seeking, assuming the server resource supports it. 假设服务器资源支持HEAD就是专门为您要寻找的内容而设计的。

If server doesn't support HEAD , you could potentially play with Range header to just request a few bytes, but that also depends on resource supporting it, and is kind of a hack. 如果服务器不支持HEAD ,则可能会使用Range标头来仅请求几个字节,但这还取决于支持它的资源,这是一种黑客。

You can use Headers property of HttpRequest 您可以使用HttpRequest的Headers属性

response.Headers

to get the headers for the response. 获取响应的标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM