0

Is there an opportunity to make http get request, read headers and not fetching body in order to reduce traffic and increase speed? 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?

4
  • 2
    You should learn about HEAD requests. The response body is (at least partially) already on its way with a GET. You can ignore it, but it still comes and costs unnecessary bandwidth.
    – spender
    Commented May 31, 2017 at 14:21
  • @spender also a good point, though reading headers and then dropping the connection would likely avoid most body (if body is large enough) to actually be transferred on the wire. That certainly would be a bad practice and hacky.
    – LB2
    Commented May 31, 2017 at 14:25
  • 1
    @LB2 : Dropping the connection has hidden costs too. It breaks HTTP 1.1 pipelining and, when you're really thrashing things, the port can't be reused during the TIME_WAIT period which can lead to port exhaustion.
    – spender
    Commented May 31, 2017 at 14:28
  • @spender - Agreed. That's exactly what I meant by bad practice. It's a bad idea to drop connections to avoid traffic. That's why made the comment to help prevent others from coming up with that idea.
    – LB2
    Commented May 31, 2017 at 14:32

2 Answers 2

9

Using GET on a resource that returns a body implies that you want to get the body. HEAD is designed exactly for what you're seeking, assuming the server resource supports it.

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.

2

You can use Headers property of HttpRequest

response.Headers

to get the headers for the response.

1
  • 4
    This let's you read the headers that OP is already doing. That doesn't stop body from being transferred to reduce traffic.
    – LB2
    Commented May 31, 2017 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.