2

pretty much...i want to do something like this:

Stream Answer = WebResp.GetResponseStream();
Response.OutputStream = Answer;

Is this possible?

1
  • 1
    Are you trying to proxy a web request? Commented May 7, 2010 at 18:24

2 Answers 2

6

No, but you can of course copy the data, either synchronously or asynchronously.

  • Allocate a buffer (like 4kb in size or so)
  • Do a read, which will either return the number of bytes read or 0 if the end of the stream has been reached
  • If data was received, write the amount read and loop to the read

Like so:

using (Stream answer = webResp.GetResponseStream()) {
    byte[] buffer = new byte[4096];
    for (int read = answer.Read(buffer, 0, buffer.Length); read > 0; read = answer.Read(buffer, 0, buffer.Length)) {
        Response.OutputStream.Write(buffer, 0, read);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

This answer has a method CopyStream to copy data between streams (and also indicates the built-in way to do it in .NET 4).

You could do something like:

using (stream answer = WebResp.GetResponseStream())
{
    CopyStream(answer, Response.OutputStream); 
    Response.Flush();
}

Comments

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.