How can I perform a GET request without downloading the content?

前端 未结 3 1169
后悔当初
后悔当初 2021-01-31 09:40

I am working on a link checker, in general I can perform HEAD requests, however some sites seem to disable this verb, so on failure I need to also perform a G

3条回答
  •  花落未央
    2021-01-31 10:14

    Couldn't you use a WebClient to open a stream and read just the few bytes you require?

    using (var client = new WebClient())
            {
                using (var stream = client.OpenRead(uri))
                {
                    const int chunkSize = 100;
                    var buffer = new byte[chunkSize];
                    int bytesRead;
                    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        //check response here
                    }
                }
            }
    

    I am not sure how WebClient opens the stream internally. But it seems to allow partial reading of data.

提交回复
热议问题