加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 下载前1000个字节

发布时间:2020-12-15 18:00:17 所属栏目:百科 来源:网络整理
导读:我需要使用C#从互联网上下载文本文件.文件大小可能非常大,我需要的信息总是在前1000个字节内.这可能吗? 解决方法 从 here被盗. string GetWebPageContent(string url){ string result = string.Empty; HttpWebRequest request; const int bytesToGet = 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);
        }

    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读