Pregunta

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();
            }
        }
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top