tags:

views:

135

answers:

4

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;
}
+1  A: 

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

rerun
+1  A: 

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);
    }
}
Thomas Levesque
That stream writes to a char[]
Luke Schafer
@Luke Schafer, what do you mean ? "that" stream is not different from any other stream. The Stream.Read method takes a byte[], not a char[]. Check the doc before you downvote...
Thomas Levesque
You must be confusing with StreamReader.Read. I'm not using StreamReader here...
Thomas Levesque
I was confusing it, my apologies. I mistook the surrounding code as being the same as the code in the OP. You are correct.
Luke Schafer
Please edit your answer slightly so I can unvote
Luke Schafer
I just did, I fixed a small mistake at the same time...
Thomas Levesque
Double win for you then :)
Luke Schafer
A: 

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
Xaqron
With that solution, if the response is shorter than 1000 bytes it will loop forever...
Thomas Levesque
!= (result !=0) added. Thanks
Xaqron