是否可以在C#中仅下载文件的一部分(例如,前100KB)?

时间:2010-08-09 18:57:14

标签: c# .net networking httpwebrequest httpwebresponse

我只是想知道这是否可行 - 我知道如何下载文件,但是如何只下载文件的前100KB?

1 个答案:

答案 0 :(得分:8)

试试这个:

        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;
        }

here被盗。