The following line in the example above:
res.Write(reader.ReadToEnd());
It is equivalent to:
string responseString = reader.ReadToEnd(); res.Write(responseString);
those. the entire web response is downloaded and stored in the line before it is sent to the client.
Instead, you should use something like the following:
Stream responseStream = response.GetResponseStream(); byte[] buffer = new byte[32768]; while (true) { int read = responseStream.Read(buffer, 0, buffer.Length); if (read <= 0) return; context.Response.OutputStream.Write(buffer, 0, read); }
(Stream copy code taken from Best way to copy between two Stream instances - C # )
This copies the stream in 32 KB blocks - you might want to reduce the size of this fragment.
Justin
source share