Search code examples
c#httpwebrequesthttpwebresponse

Wrapper class to download image + Stream was not readable


I have read many articles but im not sure if this can be resolved the way i have constructed this wrapper class to make WebRequests etc.

Heres the code - this is some of the code from my wrapper class (only the relevant section is here)

        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
            {

            }
        }

I would like to download an image from a URL so i call the wrapper class function here as below in a web page after instantiating an instance of the above 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);
        }
    }
}

The line

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

returns back the error

Exception Details: System.ArgumentException: Stream was not readable.

So after further research it seems that ive not closed/flushed the StreamReader which i have but im not sure if ive missed off anything else or if its just not possible to do it this way?


Solution

  • I think what you want to do is expose the stream as the result of the SendRequest method. I think you're ok not disposing of the HttpWebResponse itself since I think that only cleans up the response stream if you make sure to dispose of the stream that is being returned.

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

    Then you can consume it as

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