如何读取 HttpWebReqest 中的 ConnectStream
当我尝试读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
响应流只能读取一次。您需要使用
MemoryStream
进行所有处理。The response stream can only be read once. You need to use a
MemoryStream
for all processing.