dataStream.Length 和 .Position 引发了“System.NotSupportedException”类型的异常

2024-01-02

我正在尝试使用 http post 将一些数据从 ASP.NET 发布到 Web 服务。

这样做时我收到了所附的错误。我检查了很多帖子,但没有什么对我真正有帮助。对此的任何帮助将不胜感激。

Length = 'dataStream.Length' 引发了类型为 'System.NotSupportedException' 的异常

Position = 'dataStream.Position' 引发了 'System.NotSupportedException' 类型的异常

请附上我的代码:

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;
}

提前致谢 !!

Ratika


你打电话时HttpWebResponse.GetResponseStream http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx,它返回一个Stream执行 http://msdn.microsoft.com/en-us/library/system.io.stream.aspx没有任何记忆能力;也就是说,从HTTP服务器发送过来的字节直接发送到这个流中进行消费。

这不同于说FileStream实例 http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx因为如果您想读取已经通过流消耗的文件的一部分,则磁盘头始终可以移回到读取文件的位置(很可能,它缓冲在内存中,但是您明白这点)。

对于 HTTP 响应,您实际上必须reissue向服务器发出请求,以便再次获得响应。由于该响应不能保证相同,因此大多数与位置相关的方法和属性(例如Length http://msdn.microsoft.com/en-us/library/system.io.stream.length.aspx, Position http://msdn.microsoft.com/en-us/library/system.io.stream.position, Seek http://msdn.microsoft.com/en-us/library/system.io.stream.seek)在Stream实现传回给你抛出一个NotSupportedException http://msdn.microsoft.com/en-us/library/system.notsupportedexception.aspx.

如果您需要向后移动Stream,那么你应该创建一个MemoryStream实例 http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx并复制响应Stream进入MemoryStream通过CopyTo method http://msdn.microsoft.com/en-us/library/dd782932.aspx,像这样:

using (var ms = new MemoryStream())
{
    // Copy the response stream to the memory stream.
    webRequest.GetRequestStream().CopyTo(ms);

    // Use the memory stream.
}

请注意,如果您没有使用 .NET 4.0 或更高版本(其中CopyTo on the Stream类被引入)然后你可以手动复制流 https://stackoverflow.com/a/230141/50776.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

dataStream.Length 和 .Position 引发了“System.NotSupportedException”类型的异常 的相关文章

随机推荐