简体   繁体   English

寻找一种方法重写HttpWebResponse的GetResponseStream方法

[英]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. 我正在寻找一种方法来覆盖HttpWebResponse类的GetResponseStream方法,以便返回自定义流。 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. 这是在与Web服务集成的上下文中完成的,我需要从响应流中剥离一些内容。 Any thoughts? 有什么想法吗?

I am looking for a way to override the GetResponseStream method of HttpWebResponse class, in order to have a custom stream returned. 我正在寻找一种方法来覆盖HttpWebResponse类的GetResponseStream方法,以便返回自定义流。

Cool. 凉。 The GetResponseStream is virtual anyways, so go ahead, override it and return whatever you want. 无论如何, GetResponseStream是虚拟的,因此继续进行操作,重写它并返回您想要的任何内容。 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. 当然,您将必须以这种方式设计代码,以使您不必使用HttpWebResponse类的具体实现,而这种情况很少见。 Coz you usually you get an HttpWebResponse instance from an HttpWebRequest. 因为您通常会从HttpWebRequest获得HttpWebResponse实例。 So that's tight coupling. 这就是紧密耦合。 So start thinking about how you could abstract that entire HTTP stuff in your code. 因此,开始考虑如何在代码中抽象整个HTTP内容。 I mean stop thinking in terms of HttpWebRequests. 我的意思是停止考虑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. 这个答案的重点是您的代码应该以这样的方式设计,使其不依赖于任何HttpWebRequest具体类。 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. 一旦在接口后面抽象了Web服务调用,您就可以很轻松地在单元测试中模拟该调用以测试系统的实际行为,而无需依赖测试中的特定类。

You can't easily override HttpWebResponse 's behavior. 您不能轻易覆盖HttpWebResponse的行为。 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. 您可以按照评论中的@Jon的方式将Web服务API包装在客户端,或者,如果您真的想冒险,请更改请求 ,将其指向将根据需要修改流的代理服务器。

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. 假设您可以修改调用GetResponseStream的代码,那么Adapter模式可能不是一个坏选择。

class MyCustomStream : Stream { Stream originalStream; 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(); var myStream = request.GetResponseStream();

// with.... //与...。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HttpWebResponse :: GetResponseStream方法时接收不完整的数据 - Receiving incomplete data when using HttpWebResponse::GetResponseStream method HttpWebResponse GetResponseStream 挂在 Dispose - HttpWebResponse GetResponseStream hanging in Dispose HttpWebResponse.GetResponseStream转换&lt;代替&lt;etc - HttpWebResponse.GetResponseStream converts &lt; instead of < etc HttpWebResponse.GetResponseStream()(似乎是)失败,没有异常 - HttpWebResponse.GetResponseStream() (seems to be) failing with no exception 无法将HttpWebResponse.GetResponseStream()分配给Stream - Unable to assign HttpWebResponse.GetResponseStream() to Stream HttpWebResponse.GetResponseStream():什么时候传输响应体? - HttpWebResponse.GetResponseStream(): when is the response body transmitted? System.Net.HttpWebResponse.GetResponseStream()在WebException中返回截断的主体 - System.Net.HttpWebResponse.GetResponseStream() returns truncated body in WebException 从c#中的HTTPWebResponse.GetResponseStream()上传到S3 - Upload to S3 from HTTPWebResponse.GetResponseStream() in c# 有没有办法用反射“覆盖”方法? - Is there a way to “override” a method with reflection? 为什么HttpWebResponse上没有Dispose方法 - why is there no Dispose method on HttpWebResponse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM