c# – 下载前1000个字节

前端之家收集整理的这篇文章主要介绍了c# – 下载前1000个字节前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用C#从互联网上下载文本文件.文件大小可能非常大,我需要的信息总是在前1000个字节内.这可能吗?

解决方法

here被盗.
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 (Stream stream = response.GetResponseStream())
        {
            byte[] buffer = new byte[1024];
            int read = stream.Read(buffer,1000);
            Array.Resize(ref buffer,read);
            return Encoding.ASCII.GetString(buffer);
        }

    }
}

(根据评论中的要求编辑……;))

原文链接:https://www.f2er.com/csharp/97221.html

猜你在找的C#相关文章