Download the first 1000 bytes

后端 未结 2 1309
囚心锁ツ
囚心锁ツ 2021-01-05 10:11

I need to download a text file from the internet using C#. The file size can be quite large and the information I need is always within the first 1000 bytes. Is this possibl

2条回答
  •  长情又很酷
    2021-01-05 10:22

    I did this as an answer to your newer question. You could put the range header in too if you want, but I excluded it.

        string GetWebPageContent(string url)
        {
            //string result = string.Empty;
            HttpWebRequest request;
            const int bytesToGet = 1000;
            request = WebRequest.Create(url) as HttpWebRequest;
            var buffer = new char[bytesToGet];
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    sr.Read(buffer, 0, bytesToGet);
                }
            }
            return new string(buffer);
        }
    

提交回复
热议问题