开发者

Is it possible to download only one part of a file (e.g, the first 100KB) in C#?

开发者 https://www.devze.com 2023-01-11 07:12 出处:网络
I\'m just curious about whether this is possible - I know how to download a file, but how can I download only the first开发者_开发知识库 100KB of a file?Try this:

I'm just curious about whether this is possible - I know how to download a file, but how can I download only the first开发者_开发知识库 100KB of a file?


Try this:

        string GetWebPageContent(string url)
        {
            string result = string.Empty;
            HttpWebRequest request;
            const int bytesToGet = 1000;
            request = WebRequest.Create(url) as HttpWebRequest;

//get first 1000 bytes
            request.AddRange(0, bytesToGet - 1);

// the following code is alternative, you may implement the function after your needs
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                }
            }
            return result;
        }

Stolen from here.

0

精彩评论

暂无评论...
验证码 换一张
取 消