Solved

Download files using WebRequest

Posted on 2013-12-11
2
231 Views
Last Modified: 2013-12-26
webclient.DownloadFile times out and fails.


This codes below appears to work, but the resulting files are all the same size ( 39kb) , and
are not complete.  
Can someone see the error?
Thanks

         private static void DownloadFile(Uri remoteUri , string localPath)
        {
            var request = (HttpWebRequest)WebRequest.Create(remoteUri);
            request.Timeout = 30000;
            request.AllowWriteStreamBuffering = false;

            using (var response = (HttpWebResponse)request.GetResponse())
            using (var s = response.GetResponseStream())
            using (var fs = new FileStream(localPath, FileMode.Create))
            {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, bytesRead);
                    bytesRead = s.Read(buffer, 0, buffer.Length);
                }
            }
        }
0
Comment
Question by:awalkinthepark
2 Comments
 

Author Comment

by:awalkinthepark
Comment Utility
nevermind - I have webclient.DownloadFile working.
But a solution could be worth posting.
Thanks
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Add a call to Flush outside of your while loop.

e.g.

...

while ((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0)
{
    fs.Write(buffer, 0, bytesRead);
    bytesRead = s.Read(buffer, 0, buffer.Length);
}

fs.Flush();

...

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# primary key 9 53
the name DateAdd does not exist in this context 2 31
C# Update Status Bar.... 1 22
C# SQL BULK INSERT CLASS 5 32
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now