?
Solved

FtpWebRequest Not Working?

Posted on 2010-08-17
2
Medium Priority
?
624 Views
Last Modified: 2012-05-10
Windows Service project: I'm using a foreach to download files from three different ftp directories. I don't receive any errors, ftp just stops working after trying to get the first file. I know the url is corrent and I have downloaded the files by hand. Now I'm trying to automate the file downloads. Please check-out this code and give me suggestions. Note: Also running 'Download' on a seperate thread did not make any difference.

private void Download(string ftpSource, string localDestination)
        {                      
            try
            {                
                string uri = "ftp://ftp.microsoft.com/Softlib" + ftpSource;
                                     
                FtpWebRequest ftp;                
                ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));                    
                ftp.KeepAlive = false;                
                ftp.Method = WebRequestMethods.Ftp.DownloadFile;                                
                ftp.UseBinary = true;
                ftp.Proxy = null;                
                ftp.UsePassive = false;
                FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();

                Stream responseStream = response.GetResponseStream();
                FileStream writeStream = new FileStream(localDestination, FileMode.Create);                
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);    
         
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }              
 
                writeStream.Close();
                response.Close();
            }
            catch (WebException wex)
            {
                error("Download WebException: " + wex.ToString());
            }
            catch (Exception ex)
            {
                error("Download Exception: " + ex.ToString());
            }
        }
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Expert Comment

by:truromeo4juliet
ID: 33458604
what about changing

while (bytesRead > 0)

to

while (bytesRead >= 0)
0
 

Accepted Solution

by:
pointeman earned 0 total points
ID: 33459000
That didn't make any difference, but removing this made everything work. I have found this very code example on the Internet too.
  writeStream.Close();
                response.Close();
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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