有没有一种方法可以在不影响 .NET 中底层响应流的情况下检查 WebResponse?

发布于 2024-09-29 14:05:58 字数 308 浏览 0 评论 0原文

调用初始 HttpWebResponse.GetResponseStream() 并读取流后,该流已完成且无法重用。

我遇到一种情况,我需要检查响应的内容,如果它是特定数据,则获取另一个页面,然后将新响应传递下去。否则,按原样传递原始响应。唯一的问题是,在检查响应以检查此“特殊数据”之后,该响应对下游代码没有好处。

我能想到的,要使其对下游代码透明,唯一的方法是创建 HttpWebResponse 的派生类,并以某种方式缓存流式传输的数据,并将缓存的流而不是初始流向下传递。我不确定这是否可行,因为我没有进一步研究。

有没有其他方法来处理这样的情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福不弃 2024-10-06 14:05:59

这看起来可能是消息检查器的正确位置。您可以覆盖 AfterReceiveRequest 以查找相关的“某些数据”。如果没有“某些数据”,则将副本放回到消息正文中并离开,否则您可能想要引发一个事件或抛出一个代码将捕获的异常,然后“去获取其他页面”。

使用消息检查器,您可以查看传入/传出数据,根据需要进行修改,或者将其原封不动地传递到原始目的地。

注意:对于消息检查器和自定义行为,您需要 .NET 3.0 或更高版本。

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.

红玫瑰 2024-10-06 14:05:58

怎么样:

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

如果您不想将整个响应加载到内存中,您可以将其传递给某个解析函数,该函数将检查一次并返回一个包含您感兴趣的所有属性的对象,以便您可以检查该对象只要你愿意。

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.

萤火眠眠 2024-10-06 14:05:58

您是否尝试过将流的位置设置回0

处理完流后,光标位于流的末尾。这就是为什么当 ASP.NET 开始处理流时没有任何内容可读取的原因。尝试将光标移动到流的开头 - 然后 ASP.NET 可以读取整个请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文