.NET Framework Developer Center > .NET Development Forums > Network Class Library (System.Net) > Failed to read entire data from HttpWebResponse.GetResponseStream( )
Ask a questionAsk a question
 

QuestionFailed to read entire data from HttpWebResponse.GetResponseStream( )

  • Tuesday, October 27, 2009 2:35 PMKiran Reddy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,
           I am getting data from a webservice using HTTP GET. The data is around 3.5 MB.  This is the code that does this:
          
            try
            {
                    WebResponse response = res.Request.EndGetResponse(state);
                    MemoryStream ms = new MemoryStream();
                    ThreadPool.RegisterWaitForSingleObject(state.AsyncWaitHandle, new WaitOrTimerCallback(killRequestStream), state, 5000, true);

                    using (response)
                    {
                        Stream stream = GetResponseStream( (HttpWebResponse) response );

                        using (stream)
                        {
                            int size = asyncResponseBufferSize;

                            byte[] result = new byte[size];
                            int length = result.Length;

                            int read;
                            while (true)
                            {
                                read = stream.Read(result, 0, length);
                                if (read == 0)
                                {
                                    break;
                                }
                                ms.Write(result, 0, read);
                            }

                            if( ms.Length < response.ContentLength   ) System.Diagnostics.Debugger.Break(  );

                            stream.Close();
                            response.Close();
                        }
                    }

                    res.Request = null;
                }
                catch( WebException ex )
                {
     
                }

    The issue is - reading data from response stream sometimes ( not always ) end without reading entire data.
    I set a Debugger.Break() when this happened and tried setting the next statement to continue reading and now it throws exception: "Unable to read data from the transport connection: The connection was closed."

    I am not able figure out the issue. Your help is highly appreciated. Framework used - .Net 3.5.


    Regards
    Kiran

     

All Replies