简体   繁体   中英

Looking for a way to override the GetResponseStream Method of HttpWebResponse

I am looking for a way to override the GetResponseStream method of HttpWebResponse class, in order to have a custom stream returned. Essentially my goal is to modify the stream. Not sure if that is possible? This is being done in the context of integrating with a web service and i need to strip away some of the content from the response stream. Any thoughts?

I am looking for a way to override the GetResponseStream method of HttpWebResponse class, in order to have a custom stream returned.

Cool. The GetResponseStream is virtual anyways, so go ahead, override it and return whatever you want. Of course you will have to design your code in such a manner so that you are not working with concrete implementations of the HttpWebResponse class which is seldom the case. Coz you usually you get an HttpWebResponse instance from an HttpWebRequest. So that's tight coupling. So start thinking about how you could abstract that entire HTTP stuff in your code. I mean stop thinking in terms of HttpWebRequests. Start thinking in terms of interfaces and abstractions in your code. That's what's gonna save you anyways and that's what's gonna weaken the coupling between the different layers of your code and make it unit testable.

The point of this answer is that your code should be designed in such a way so that it doesn't depend on any HttpWebRequest concrete classes. You should start thinking in terms of hiding this behind an interface abstraction that you could easily mock in your unit test.

Once you abstract your web service call behind an interface you could very easily mock this call in your unit test to test the actual behavior of the system without relying on specific classes in your test.

You can't easily override HttpWebResponse 's behavior. Either you wrap the web-service API on the client side, as @Jon suggested in the comment, or, if you really want to get adventurous, change the request , pointing it to a proxy server that will modify the stream as required.

It's not going to be trivial, though.

Assuming you can modify the code calling GetResponseStream, the Adapter pattern might not be a bad choice.

class MyCustomStream : Stream { Stream originalStream;

  MyCustomStream(Stream originalStream) 
  { this.originalStream = originalStream; }

  override int Read(byte [] buffer, int offset, int count) 
  {
      byte [] temp = new byte[count];
      var ret = originalStream.Read(temp, offset, count);
      // modify your buffer if desired
      Array.Copy(buffer, temp);
      return ret;
  }

  // implement all the other abstract methods of Stream and just call originalStream 

}

// now replace code like this....

var myStream = request.GetResponseStream();

// with....

var myStream = new MyCustomStream(request.GetResponseStream());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM