C# dataStream.Length 和 .Position 抛出了“System.NotSupportedException”类型的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10604774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dataStream.Length and .Position threw an exception of type 'System.NotSupportedException'
提问by NewGirlInCalgary
I am trying to post some data from asp.net to webservice using http post.
我正在尝试使用 http post 将一些数据从 asp.net 发布到 webservice。
While doing that I am getting the enclosed error. I have checked many post but nothing helps me really. Any help onto this will greatly appreciated.
在这样做时,我收到了包含的错误。我检查了很多帖子,但没有什么能真正帮助我。对此的任何帮助将不胜感激。
Length = 'dataStream.Length' threw an exception of type 'System.NotSupportedException'
Length = 'dataStream.Length' 抛出了类型为 'System.NotSupportedException' 的异常
Position = 'dataStream.Position' threw an exception of type 'System.NotSupportedException'
Position = 'dataStream.Position' 抛出了一个类型为 'System.NotSupportedException' 的异常
Enclosed please my code:
附上我的代码:
public XmlDocument SendRequest(string command, string request)
{
XmlDocument result = null;
if (IsInitialized())
{
result = new XmlDocument();
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
try
{
string prefix = (m_SecureMode) ? "https://" : "http://";
string url = string.Concat(prefix, m_Url, command);
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml";
webRequest.ServicePoint.Expect100Continue = false;
string UsernameAndPassword = string.Concat(m_Username, ":", m_Password);
string EncryptedDetails = Convert.ToBase64String(Encoding.ASCII.GetBytes(UsernameAndPassword));
webRequest.Headers.Add("Authorization", "Basic " + EncryptedDetails);
using (StreamWriter sw = new StreamWriter(webRequest.GetRequestStream()))
{
sw.WriteLine(request);
}
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
webResponse = (HttpWebResponse)webRequest.GetResponse();
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
result.Load(sr.BaseStream);
sr.Close();
}
}
catch (Exception ex)
{
string ErrorXml = string.Format("<error>{0}</error>", ex.ToString());
result.LoadXml(ErrorXml);
}
finally
{
if (webRequest != null)
webRequest.GetRequestStream().Close();
if (webResponse != null)
webResponse.GetResponseStream().Close();
}
}
return result;
}
Thanks in advance !!
提前致谢 !!
Ratika
拉蒂卡
回答by casperOne
When you call HttpWebResponse.GetResponseStream, it returns a Streamimplementationthat that doesn't have any recall ability; in other words, the bytes that are sent from the HTTP server are sent directly to this stream for consumption.
当你调用HttpWebResponse.GetResponseStream,它返回一个Stream实现是在没有任何召回的能力; 换句话说,从 HTTP 服务器发送的字节将直接发送到此流以供消费。
This is different from say, a FileStreaminstancein that if you want to read a section of the file that you've already consumed through the stream, the disk head can always be moved back to the location to read the file from (more than likely, it's buffered in memory, but you get the point).
这不同于说,一个FileStream实例,因为如果您想读取您已经通过流消耗的文件的一部分,磁盘磁头总是可以移回到从中读取文件的位置(很可能,它在内存中缓冲,但你明白了)。
With an HTTP response, you'd have to actually reissuethe request to the server in order to get the response again. Because that response is not guaranteed to be the same, most of the position related methods and properties (e.g. Length, Position, Seek) on the Streamimplementation passed back to you throw a NotSupportedException.
对于 HTTP 响应,您实际上必须向服务器重新发出请求才能再次获得响应。因为不能保证该响应是相同的,所以传递回给您的实现中大多数与位置相关的方法和属性(例如Length、Position、Seek)Stream都会抛出一个NotSupportedException.
If you need to move backwards in the Stream, then you should create a MemoryStreaminstanceand copy the response Streaminto the MemoryStreamthrough the CopyTomethod, like so:
如果您需要在 中向后移动Stream,那么您应该创建一个MemoryStream实例并通过方法将响应复制Stream到 中,如下所示:MemoryStreamCopyTo
using (var ms = new MemoryStream())
{
// Copy the response stream to the memory stream.
webRequest.GetRequestStream().CopyTo(ms);
// Use the memory stream.
}
Note, if you aren't using .NET 4.0 or later (where CopyToon the Streamclass was introduced) then you can copy the stream manually.
请注意,如果你没有使用.NET 4.0或更高版本(其中CopyTo在Stream引入类),那么你可以手动复制流。

