开发者

Download the first 1000 bytes of a file using C# [duplicate]

开发者 https://www.devze.com 2023-01-19 07:15 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Download the first 1000 bytes
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Download the first 1000 bytes

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

This is what I have so far. I found out that the server might ignore the range header. Is there a 开发者_运维技巧way to limit streamreader to only read the first 1000 characters?

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


Please follow-up in your question from yesterday!


There is a read method that you can specify the number of characters to read.


You can retrieve the first 1000 bytes from the stream, then decode the string from the bytes:

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


Instead of using request.AddRange() which may be ignored by some servers as you said, read 1000 bytes (1 KB = 1024 bytes) from stream and then close it. This is like you get disconnected from server after receiving 1000 bytes. Code:

 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号