views:

36

answers:

3

After a call to initial HttpWebResponse.GetResponseStream() and reading through the stream, that stream is done for and cannot be reused.

I have a situation where I need to examine the content of the response and if it is of a certain data, get another page and then pass the new response down the line. Otherwise, pass down the original response as is. The only problem is that after examining the response to check for this "special data", that response is no good to the downstream code.

The only way, I can think of, to make this transparent to the downstream code, is to create a derived class of HttpWebResponse, and somehow cache the data streamed, and pass that cached stream down the line instead of the initial stream. I'm not sure if that's even feasible since I haven't looked into it further.

Are there any alternative ways to handle a scenario like this?

+3  A: 

How about this:

using (var client = new WebClient())
{
    string result = client.DownloadString("http://www.foo.bar");
    // TODO: examine result as much as you wish
}

If you don't want to load the whole response into memory you could pass it to some parsing function which will examine it once and return an object containing all the properties you are interested in so that after you could examine this object as much as you wish.

Darin Dimitrov
Chris
I agree that people should use WebClient more. But in my case, I can't use WebClient since I need to build the request myself. WebClient is good for simple things but not for complex requests. And the request being complex, it's not only cumbersome to handle this in the client side - i.e. re-creating another request. What I am going after is being transparent to the client code as much as possible.
Jiho Han
+1  A: 

Have you tried setting the Position of the stream back to 0?

After you've processed the stream the cursor is at the end of the stream. That's why when the ASP.NET starts processing the stream there's nothing to read. Try moving the cursor to the beginning of the stream - then ASP.NET can read whole request.

Jakub Konecki
The streams returned by WebRequest and WebResponse are not seekable - you cannot rewind it. That was the first thing I tried, believe me.
Jiho Han
@Jiho Han - I think I did this with HttpContext.Request. Sorry it didn't work...
Jakub Konecki
+1  A: 

This looks like it might be the right place for a message inspector. You'd overwrite the AfterReceiveRequest to look for your relevant "certain data." If no "certain data" then put the copy back into the message body and go away, otherwise you might want to raise an event or throw an exception that your code would catch and then "go get that other page."

With a message inspector, you can look at incoming/outgoing data, modify it if you want, or pass it unaltered to the original destination.

Note: for message inspectors and custom behaviors, you'll need .NET 3.0 or higher.

Tangurena
Ah... looks like it's a WCF feature. Unfortunately, we are still on 2.0 here.
Jiho Han