使用C#下载文件的前1000个字节

时间:2010-10-05 22:00:31

标签: c# .net

  

可能重复:
  Download the first 1000 bytes

我需要使用C#从互联网上下载文本文件。文件大小可以很大,我需要的信息总是在前1000个字节内。

这是我到目前为止所拥有的。我发现服务器可能会忽略范围标头。有没有办法限制streamreader只读取前1000个字符?

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

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

有一个read method可以指定要读取的字符数。

答案 2 :(得分:1)

您可以从流中检索前1000个字节,然后从字节中解码字符串:

using (WebResponse response = request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        byte[] bytes = new byte[bytesToGet];
        int count = stream.Read(bytes, 0, bytesToGet);
        Encoding encoding = Encoding.GetEncoding(response.Encoding);
        result = encoding.GetString(bytes, 0, count);
    }
}

答案 3 :(得分:0)

如您所说,不使用可能被某些服务器忽略的request.AddRange(),而是从流中读取1000字节(1 KB = 1024字节),然后将其关闭。这就像您在收到1000个字节后从服务器断开连接。代码:

 int count = 0;
 int result = 0;
 byte[] buffer = new byte[1000];
 // create stream from URL as you did above

 do
 {
     // we want to read 1000 bytes but stream may read less. result = bytes read
     result = stream.Read(buffer, 0, 1000); // Use try around this for error handling
     count += result;
 } while ((count < 1000) && (result != 0));
 stream.Dispose();
 // now buffer has the first 1000 bytes of your request