Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This is my console application where i am trying to download file form the rest webservice.
But it is not downloading all content of the file my file size of 2.25GB, some time it 103 MB or 1.75GB.
This line of code throw exception
"InternalSocket = '((System.Net.ConnectStream)(stream)).InternalSocket' threw an exception of type 'System.ObjectDisposedException'".
stream.Read(buffer, 0, buffer.Length)


C#
public void DownloadFile()
        {
            var product = new object();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(SERVER - URL, "download/LgFiles"));
            request.Method = "POST";
            //Add for testing
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("fileid", product.fileid);
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
            request.ServicePoint.ConnectionLimit = 1;
            request.ServicePoint.Expect100Continue = false;
            FileDTO file = new FileDTO();
            file.fileid = product.fileid;
            string path = @"C:\try\ZipDownloads\" + product.fulfilled;

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                streamWriter.Write(string.Concat("obj=", new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(file)));

            try
            {
                var response = request.GetResponse();

                long counter = 1;
                long dwnlddSize = 0;

                using (Stream stream = response.GetResponseStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        // Stream the response to a MemoryStream via the byte array buffer
                        Byte[] buffer = new Byte[16348];
                        Int32 bytesRead;
                        Stopwatch watch = new Stopwatch();
                        using (FileStream fs = new FileStream(path + "\\" + product.filename, FileMode.CreateNew, FileAccess.Write))
                        {
                            watch.Start();
                            while (dwnlddSize < long.Parse(product.filesize))
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                fs.Write(buffer, 0, bytesRead);
                                dwnlddSize += bytesRead;
                            }
                            Console.WriteLine("TimeTaken is " + watch.Elapsed.ToString());
                        }
                    }
                }
            }
            catch
            {

            }
        }
Posted
Comments
Bernhard Hiller 8-Sep-14 3:32am    
What does that 'MemoryStream memoryStream ' do? I do not see it used in your code. Remove it!
Also, for such big files, I'd prefer larger chunks of data than 16348 bytes.
rahul_ahuja420 8-Sep-14 8:38am    
I removed memoeryStream and also increased byte size 32768 bytes but still getting the same issue. Stream object throw Exception

Message = "Cannot access a disposed object.\r\nObject name: 'System.Net.TlsStream'."
InternalSocket = '((System.Net.ConnectStream)(stream)).InternalSocket' threw an exception of type 'System.ObjectDisposedException'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900