0

I would like to read only part of an HttpWebResponse. Let's say the first 100k. How can I read only the first 100k of the Response but still end up with a non-corrupted substring? If I just throw the first 100k into a byte[] I believe I could end up with corrupted data.

        HttpWebRequest request = HttpWebRequest.Create("http://www.yahoo.com") as HttpWebRequest;

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        using (Stream responseStream = response.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(responseStream))
            {
                string content = sr.ReadToEnd();
            }
        }
BowserKingKoopa
  • 2,701
  • 3
  • 33
  • 40
  • If you are only interested in part of the response stream it would make sense to only request that part of the stream. [Partial Content](http://benramsey.com/blog/2008/05/206-partial-content-and-range-requests/). Of course, partial requests are not supported on 100% of servers. – Aron Mar 12 '13 at 01:53
  • Not sure what is your question - read strings till you are happy instead of `ReadToEnd`... Or you have some other particular condition ? – Alexei Levenkov Mar 12 '13 at 02:00
  • The first 100k bytes will always be the same no matter how you read them. Whether those first 100K byte hold any valid meaning depends on what the http server is providing you with. – TimothyP Mar 12 '13 at 02:00

1 Answers1

0

You cannot expect to get non-corrupted substring by limiting the size by the length of bytes.

A better way would be reading by characters (Read, ReadBlock, ReadLine,) until you are satisfied.

Terry
  • 299
  • 1
  • 5
  • 12