Slow Downloading

R

rony_16

Hi,
I have a problem downloading a file .
after i connect to the website and get the stream , i treing to write
the file on the HD.
public void SaveStreamToFile(string filePath, Stream stream)
{
// If not all data was provided return false;
//if (string.IsNullOrEmpty(filePath) || stream == null)
// return false;
// System.IO.Stream gzStream = stream; //= new
System.IO.Stream()//(stream,
System.IO.Compression.CompressionMode.Decompress);
FileStream fs = null;
try
{
byte[] inBuffer = new byte[10000];
int bytesRead = 0;
fs = File.Open(filePath, FileMode.Create,
FileAccess.Write, FileShare.Read);
while ((bytesRead = stream.Read(inBuffer, 0,
inBuffer.Length)) > 0)
{
fs.Write(inBuffer, 0, bytesRead);
}
fs.Flush();
// return true;
}
catch (Exception ex)
{
throw ex;
// ExceptionPolicy.HandleException(ex, "Global
Policy");
//return false;
}
finally
{
fs.Close();
}
}
webRequest1.KeepAlive = true;
webRequest1.CookieContainer = cookies;
webRequest1.Accept =
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
webRequest1.Headers.Set("Accept-Language",
"en-us,en;q=0.5");
webRequest1.Headers.Set("Accept-Encoding", "gzip,deflate");
webRequest1.Headers.Set("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
webRequest1.Headers.Set("Keep-Alive", "300");
WebResponse myResponse1 = webRequest1.GetResponse();
Stream ReceiveStream1 = myResponse1.GetResponseStream();
SaveStreamToFile(@"C:\file.csv", ReceiveStream1);

the problem is that if i want to download a file (as big as 3Mb) it
takes me 40min to do it .
(my internet connection is fast)

please suggest me , how to solve this problem .
Thanks , Rony
 
D

Daniel

Havent read the code but a quick thought, the server that is hosting the
file you are downloading, whats its upstream bandwidth?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Where is the server hosted?
What performance do you get if you try to download this same file frmo the
browser.

Are you getting any exception?
Is the file compressed when you download it? I see you setup several headers
in the request, what if you do not do it?
 
R

rony_16

hi,
when i download this file through browser, it being downloaded very
fast .
and the server is very fast .
what i do know that this file protected with SSL .


Hi,

Where is the server hosted?
What performance do you get if you try to download this same file frmo the
browser.

Are you getting any exception?
Is the file compressed when you download it? I see you setup several headers
in the request, what if you do not do it?


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



rony_16 said:
Hi,
I have a problem downloading a file .
after i connect to the website and get the stream , i treing to write
the file on the HD.
public void SaveStreamToFile(string filePath, Stream stream)
{
// If not all data was provided return false;
//if (string.IsNullOrEmpty(filePath) || stream == null)
// return false;
// System.IO.Stream gzStream = stream; //= new
System.IO.Stream()//(stream,
System.IO.Compression.CompressionMode.Decompress);
FileStream fs = null;
try
{
byte[] inBuffer = new byte[10000];
int bytesRead = 0;
fs = File.Open(filePath, FileMode.Create,
FileAccess.Write, FileShare.Read);
while ((bytesRead = stream.Read(inBuffer, 0,
inBuffer.Length)) > 0)
{
fs.Write(inBuffer, 0, bytesRead);
}
fs.Flush();
// return true;
}
catch (Exception ex)
{
throw ex;
// ExceptionPolicy.HandleException(ex, "Global
Policy");
//return false;
}
finally
{
fs.Close();
}
}
webRequest1.KeepAlive = true;
webRequest1.CookieContainer = cookies;
webRequest1.Accept =
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
webRequest1.Headers.Set("Accept-Language",
"en-us,en;q=0.5");
webRequest1.Headers.Set("Accept-Encoding", "gzip,deflate");
webRequest1.Headers.Set("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
webRequest1.Headers.Set("Keep-Alive", "300");
WebResponse myResponse1 = webRequest1.GetResponse();
Stream ReceiveStream1 = myResponse1.GetResponseStream();
SaveStreamToFile(@"C:\file.csv", ReceiveStream1);

the problem is that if i want to download a file (as big as 3Mb) it
takes me 40min to do it .
(my internet connection is fast)

please suggest me , how to solve this problem .
Thanks , Rony
 
S

shriop

Your code is pretty much correct. You're having keep alive turned on
though, and I don't see disposal code on your response. The framework
internals live by the protocol specs for https that say that you can
only have 2 current connections to the same domain over https at the
same time. If you're running this code in succession, or threading it,
you will have problems with one download preventing the other from
downloading. This is the only thing I can think of which would cause a
slow download. You can override this behavior by setting the
ServicePoint.ConnectionLimit to a higher number, like say 50 or so. I
would also suggest making absolutely sure you have using blocks on
every single declared variable that you can in there as I've seen a lot
of issues just coming from people not realizing that a stream was still
open. You can always packet sniff the download and see what's actually
going on between you and the server under the covers.

Bruce Dunwiddie
http://www.csvreader.com

rony_16 said:
hi,
when i download this file through browser, it being downloaded very
fast .
and the server is very fast .
what i do know that this file protected with SSL .


Hi,

Where is the server hosted?
What performance do you get if you try to download this same file frmo the
browser.

Are you getting any exception?
Is the file compressed when you download it? I see you setup several headers
in the request, what if you do not do it?


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



rony_16 said:
Hi,
I have a problem downloading a file .
after i connect to the website and get the stream , i treing to write
the file on the HD.
public void SaveStreamToFile(string filePath, Stream stream)
{
// If not all data was provided return false;
//if (string.IsNullOrEmpty(filePath) || stream == null)
// return false;
// System.IO.Stream gzStream = stream; //= new
System.IO.Stream()//(stream,
System.IO.Compression.CompressionMode.Decompress);
FileStream fs = null;
try
{
byte[] inBuffer = new byte[10000];
int bytesRead = 0;
fs = File.Open(filePath, FileMode.Create,
FileAccess.Write, FileShare.Read);
while ((bytesRead = stream.Read(inBuffer, 0,
inBuffer.Length)) > 0)
{
fs.Write(inBuffer, 0, bytesRead);
}
fs.Flush();
// return true;
}
catch (Exception ex)
{
throw ex;
// ExceptionPolicy.HandleException(ex, "Global
Policy");
//return false;
}
finally
{
fs.Close();
}
}
webRequest1.KeepAlive = true;
webRequest1.CookieContainer = cookies;
webRequest1.Accept =
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
webRequest1.Headers.Set("Accept-Language",
"en-us,en;q=0.5");
webRequest1.Headers.Set("Accept-Encoding", "gzip,deflate");
webRequest1.Headers.Set("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
webRequest1.Headers.Set("Keep-Alive", "300");
WebResponse myResponse1 = webRequest1.GetResponse();
Stream ReceiveStream1 = myResponse1.GetResponseStream();
SaveStreamToFile(@"C:\file.csv", ReceiveStream1);

the problem is that if i want to download a file (as big as 3Mb) it
takes me 40min to do it .
(my internet connection is fast)

please suggest me , how to solve this problem .
Thanks , Rony
 
R

rony_16

thanks , i will try it .


Your code is pretty much correct. You're having keep alive turned on
though, and I don't see disposal code on your response. The framework
internals live by the protocol specs for https that say that you can
only have 2 current connections to the same domain over https at the
same time. If you're running this code in succession, or threading it,
you will have problems with one download preventing the other from
downloading. This is the only thing I can think of which would cause a
slow download. You can override this behavior by setting the
ServicePoint.ConnectionLimit to a higher number, like say 50 or so. I
would also suggest making absolutely sure you have using blocks on
every single declared variable that you can in there as I've seen a lot
of issues just coming from people not realizing that a stream was still
open. You can always packet sniff the download and see what's actually
going on between you and the server under the covers.

Bruce Dunwiddie
http://www.csvreader.com

rony_16 said:
hi,
when i download this file through browser, it being downloaded very
fast .
and the server is very fast .
what i do know that this file protected with SSL .


Hi,

Where is the server hosted?
What performance do you get if you try to download this same file frmo the
browser.

Are you getting any exception?
Is the file compressed when you download it? I see you setup several headers
in the request, what if you do not do it?


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Hi,
I have a problem downloading a file .
after i connect to the website and get the stream , i treing to write
the file on the HD.
public void SaveStreamToFile(string filePath, Stream stream)
{
// If not all data was provided return false;
//if (string.IsNullOrEmpty(filePath) || stream == null)
// return false;
// System.IO.Stream gzStream = stream; //= new
System.IO.Stream()//(stream,
System.IO.Compression.CompressionMode.Decompress);
FileStream fs = null;
try
{
byte[] inBuffer = new byte[10000];
int bytesRead = 0;
fs = File.Open(filePath, FileMode.Create,
FileAccess.Write, FileShare.Read);
while ((bytesRead = stream.Read(inBuffer, 0,
inBuffer.Length)) > 0)
{
fs.Write(inBuffer, 0, bytesRead);
}
fs.Flush();
// return true;
}
catch (Exception ex)
{
throw ex;
// ExceptionPolicy.HandleException(ex, "Global
Policy");
//return false;
}
finally
{
fs.Close();
}
}
webRequest1.KeepAlive = true;
webRequest1.CookieContainer = cookies;
webRequest1.Accept =
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
webRequest1.Headers.Set("Accept-Language",
"en-us,en;q=0.5");
webRequest1.Headers.Set("Accept-Encoding", "gzip,deflate");
webRequest1.Headers.Set("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
webRequest1.Headers.Set("Keep-Alive", "300");
WebResponse myResponse1 = webRequest1.GetResponse();
Stream ReceiveStream1 = myResponse1.GetResponseStream();
SaveStreamToFile(@"C:\file.csv", ReceiveStream1);

the problem is that if i want to download a file (as big as 3Mb) it
takes me 40min to do it .
(my internet connection is fast)

please suggest me , how to solve this problem .
Thanks , Rony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top