如何读取 HttpWebReqest 中的 ConnectStream

发布于 2024-12-16 02:26:24 字数 1104 浏览 3 评论 0原文

当我尝试读取 HttpWebRequest 和 HttpWebResponse 类的底层流时,我遇到了困难。事实证明,这些不是内存流;而是内存流。它们是 ConnectStream 类型。这种类型的流的问题是它不支持读、写、查找,什么都不支持。每次我尝试对这种类型的流执行某些操作时,都会出现不支持的异常。

有没有办法使用其他类型的流来代替实际上可读的 ConnectStream?

代码:

public class BaseAsmxProxy : SoapHttpClientProtocol
{
    protected override System.Xml.XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)
    {
        string responseXml = GetResponseDataFromStream(message.Stream);

        return base.GetReaderForMessage(message, bufferSize);
    }

    private string GetResponseDataFromStream(System.IO.Stream stream)
    {
        string returnValue = null;

        long initialPosition = stream.Position;
        stream.Seek(0, SeekOrigin.Begin);

        StreamReader reader = new StreamReader(stream);
        returnValue = reader.ReadToEnd();

        stream.Seek(initialPosition, SeekOrigin.Begin);

        return returnValue;
    }
}

请注意,当我使用 SoapExtension(用于某些其他功能)时,它会切换流类型,并且我无意中获得了实际上可读的 MemoryStream - 这正是我所需要的。但是,我必须在某个时候关闭 SoapExtentensions,这就是问题开始发生的地方:ConnectStream 根本不可读。

I'm running into a wall when trying to read underlying streams of either HttpWebRequest and HttpWebResponse classes. As it turns out, these are not memory streams; they are of type ConnectStream. The problem with this type of stream is that it doesn't support reading, writing, seeking, nothing. Every time I try to do something with this type of stream I get not supported exceptions.

Is there a way to use some other type of stream in place of ConnectStream that would actually be readable?

Code:

public class BaseAsmxProxy : SoapHttpClientProtocol
{
    protected override System.Xml.XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)
    {
        string responseXml = GetResponseDataFromStream(message.Stream);

        return base.GetReaderForMessage(message, bufferSize);
    }

    private string GetResponseDataFromStream(System.IO.Stream stream)
    {
        string returnValue = null;

        long initialPosition = stream.Position;
        stream.Seek(0, SeekOrigin.Begin);

        StreamReader reader = new StreamReader(stream);
        returnValue = reader.ReadToEnd();

        stream.Seek(initialPosition, SeekOrigin.Begin);

        return returnValue;
    }
}

Note that when I use a SoapExtension (for some other functionality) it switches stream types and I inadvertently get MemoryStream here that is actually readable - which is exactly what I need. However, I'll have to turn off SoapExtentensions at some point and that's where the problems start to occur: ConnectStream is simply not readable.

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

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

发布评论

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

评论(1

脱离于你 2024-12-23 02:26:24

响应流只能读取一次。您需要使用 MemoryStream 进行所有处理。

The response stream can only be read once. You need to use a MemoryStream for all processing.

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