dataStream.Length和.Position引发了类型'System.NotSupportedException'的异常 [英] dataStream.Length and .Position threw an exception of type 'System.NotSupportedException'

查看:275
本文介绍了dataStream.Length和.Position引发了类型'System.NotSupportedException'的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to post some data from asp.net to webservice using http post.

在执行此操作时,我收到了随附的错误.我检查了很多职位,但没有任何帮助.任何对此的帮助将不胜感激.

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'引发了类型'System.NotSupportedException'的异常

Length = 'dataStream.Length' threw an exception of type 'System.NotSupportedException'

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

Position = 'dataStream.Position' threw an exception of type '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

推荐答案

调用

When you call HttpWebResponse.GetResponseStream, it returns a Stream implementation that 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.

这与说 FileStream不同实例,如果您想读取流中已经使用过的文件部分,则始终可以将磁盘头移回该位置以从中读取文件(大于可能,它已缓冲在内存中,但您明白了.)

This is different from say, a FileStream instance in 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).

使用HTTP响应,您实际上必须向服务器重新发出请求,以便再次获得响应.因为不能保证该响应是相同的,所以大多数与位置相关的方法和属性(例如 Stream 实现上的rel ="nofollow noreferrer"> Seek )会抛出

With an HTTP response, you'd have to actually reissue the 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 Stream implementation passed back to you throw a NotSupportedException.

如果需要在 Stream 中向后移动,则应创建 CopyTo 方法,例如:

If you need to move backwards in the Stream, then you should create a MemoryStream instance and copy the response Stream into the MemoryStream through the CopyTo method, like so:

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

    // Use the memory stream.
}

注意,如果您不使用.NET 4.0或更高版本(引入了 Stream 类上的 CopyTo ),则可以

Note, if you aren't using .NET 4.0 or later (where CopyTo on the Stream class was introduced) then you can copy the stream manually.

这篇关于dataStream.Length和.Position引发了类型'System.NotSupportedException'的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆