Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program hangs on FtpWebResponse

First time poster, long-time reader. I have a really annoying problem thats been getting on my nerves. Ive got a program set up so I listen for new files on an FTP server, if theres a new file I download it. From there I work on some of the information in the file, etc. My problem comes when I run through my sequence the second time. That is, on the first file I download everything is totally fine, but as soon as a new file gets detected and my program tries downloading it, my program just hangs.

 private static void DownloadFile(string s)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://blabla.com/"+s);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential("xxx" ,"zzz");

            using (FtpWebResponse partResponse = (FtpWebResponse)request.GetResponse())
            {
                Stream partReader = partResponse.GetResponseStream();

                byte[] buffer = new byte[1024];
                FileInfo fi = new FileInfo(path);
                FileStream memStream = fi.Create();
                while (true)
                {
                    int bytesRead = partReader.Read(buffer, 0, buffer.Length - 1);
                    if (bytesRead == 0)
                        break;

                    memStream.Write(buffer, 0, bytesRead);
                }
                partResponse.Close();
                memStream.Close();
            }
            Console.WriteLine(DateTime.Now + " file downloaded");
            MoveFileToInProgress(s);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

The line it hangs on is this one: using (FtpWebResponse partResponse = (FtpWebResponse)request.GetResponse())

The reason my method here is static is because Im just running it in a different project to test it.. My question here is, how come it only ever dies on the second file? Ive been staring myself blind for hours now!

like image 909
MartinNielsen Avatar asked Jun 01 '11 13:06

MartinNielsen


1 Answers

I ran into this problem as well... try finishing your request first and then closing it before trying to retrieve the response. That worked for me (actually tried it after reading comment by MartinNielsen). Here is what I did.

        // connect to the ftp site
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpUri);
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);

        // setting proxy to null so that it does not go through the proxy
        ftpRequest.Proxy = null;

        // get file information
        StreamReader fileStream = new StreamReader(filePath);
        byte[] fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
        ftpRequest.ContentLength = fileBytes.Length;
        fileStream.Close();

        // open connection to ftp site
        Stream ftpRequestStream = ftpRequest.GetRequestStream();

        // write the file to the stream
        ftpRequestStream.Write(fileBytes, 0, fileBytes.Length);

        // close the stream
        ftpRequestStream.Close();

        // get the response from the server
        FtpWebResponse ftpUploadResponse = (FtpWebResponse)ftpRequest.GetResponse();
        string result = ftpUploadResponse.StatusDescription;

        // close response
        ftpUploadResponse.Close();

        // return response to calling code
        return result;

Here are a couple of the resources that I used when writing this code (won't let me post more than 2, there were more)

How to: Upload Files with FTP

Uploading a file -- "The requested URI is invalid for this FTP command"

like image 155
pscapng Avatar answered Sep 19 '22 11:09

pscapng