1

How to download first 200 bytes of a file via HTTP protocol using C#?
I believed it could be done like this:

WebClient wc = new WebClient();
byte[] buffer = new byte[200];
using (var stream = wc.OpenRead(fileName))
{
     stream.Read(buffer, 0, 200);
}

but when wc.OpenRead it called it downloads the whole file.

1
  • @Jon: No, that's certainly not a duplicate as this is C# and the other is Java. Commented Jun 10, 2011 at 10:11

1 Answer 1

5

You need to set a Range Header on your WebClient before you invoke the OpenRead method.

See: http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

Could you please provide a source how to do it. I tried wc.Headers.Add("Range", "bytes=0-200"); but wc.OpenRead(fileName) throws an exception.
What is the exception being thrown, please?
I did what I needed using WebRequest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.