包装 class 以下载图像 + 流不可读

Wrapper class to download image + Stream was not readable

我已经阅读了很多文章,但我不确定是否可以按照我构造此包装器 class 来制作 WebRequests 等的方式解决这个问题

这是代码 - 这是我的包装器中的一些代码 class(这里只有相关部分)

        public Stream ResponseStream
        {
            get { return m_ResponseStream; }
        }

public SendRequest(string Url)
{
            HttpWebRequest WebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
            HttpWebResponse WebResponse = null;
            StreamReader ResponseReader = null;

            // Set default values here. Removed for clarity
            // Get Method etc

            try
            {
                using (WebResponse = (System.Net.HttpWebResponse)WebRequest.GetResponse())
                {
                    if (WebResponse != null)
                    {
                        m_StatusCode = WebResponse.StatusCode;
                        using (m_ResponseStream = WebResponse.GetResponseStream())
                        {
                            using (ResponseReader = new StreamReader(m_ResponseStream, ResponseEncoding))
                            {
                                m_Response = ResponseReader.ReadToEnd();
                            }

                            m_ResponseStream.Flush();
                            m_ResponseStream.Close();
                        }
                    }
                }

            }
            catch (WebException we)
            {
                // Handle WebException
            }
            catch (Exception ex)
            {
                // Handle Exception
            }
            finally
            {

            }
        }

我想从 URL 下载图像,因此在实例化上述 class 的实例后,我在网页中调用包装器 class 函数,如下所示:

public void DownloadImage()
{
    WebServiceWrapper wr = new WebServiceWrapper();
    wr.SendRequest("http://example.com/someimage.jpg");

    using (BinaryReader reader = new BinaryReader(wr.ResponseStream))
    {
        Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
        using (FileStream lxFS = new FileStream("34891.jpg", FileMode.Create))
        {
            lxFS.Write(lnByte, 0, lnByte.Length);
        }
    }
}

using (BinaryReader reader = new BinaryReader(wr.ResponseStream))

returns 返回错误

异常详细信息:System.ArgumentException:流不可读。

因此,经过进一步研究,我似乎没有 closed/flushed 我拥有的 StreamReader,但我不确定我是否遗漏了其他任何东西,或者是否无法以这种方式做到这一点?

我认为您想做的是将流公开为 SendRequest 方法的结果。我认为您可以不处理 HttpWebResponse 本身,因为我认为只有在您确保处理返回的流的情况下才会清理响应流。

public Stream SendRequest(string Url)
{
    HttpWebRequest WebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);

    // Set default values here. Removed for clarity
    // Get Method etc

    var WebResponse = (System.Net.HttpWebResponse)WebRequest.GetResponse();

    m_StatusCode = WebResponse.StatusCode;

    return WebResponse.GetResponseStream();
}

然后你就可以消费它了

public void DownloadImage()
{
    WebServiceWrapper wr = new WebServiceWrapper();

    using (var responseStream = wr.SendRequest("http://example.com/someimage.jpg"))
    using (BinaryReader reader = new BinaryReader(responseStream))
    {
        Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
        using (FileStream lxFS = new FileStream("34891.jpg", FileMode.Create))
        {
            lxFS.Write(lnByte, 0, lnByte.Length);
        }
    }
}