包装类下载图像+流不可读

时间:2015-02-18 20:25:39

标签: c# httpwebrequest httpwebresponse

我已阅读过很多文章,但我不确定这是否可以通过构建此包装类来构建WebRequests等来解决。

继承代码 - 这是我的包装类中的一些代码(只有相关部分在这里)

        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下载图像,所以在实例化上述类的实例后,我在网页中调用包装类函数,如下所示:

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))

返回错误

异常详细信息:System.ArgumentException:Stream无法读取。

所以经过进一步的研究后,我似乎没有关闭/刷新我所拥有的StreamReader,但我不确定我是否错过了其他任何东西,或者它是否不可能这样做?

1 个答案:

答案 0 :(得分:2)

我认为您要做的是将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);
        }
    }
}