程序挂起在FtpWebResponse上

时间:2011-06-01 13:37:57

标签: c# ftpwebresponse

第一次海报,长期读者。我有一个非常烦人的问题,让我感到紧张。我已经设置了一个程序,所以我在FTP服务器上监听新文件,如果我下载了一个新文件。从那里我处理文件中的一些信息,等等。当我第二次运行我的序列时,我的问题出现了。也就是说,在我下载的第一个文件中,一切都很好,但是一旦检测到新文件并且我的程序尝试下载它,我的程序就会挂起。

 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);
        }
    }

它挂起的线是这一行: 使用(FtpWebResponse partResponse =(FtpWebResponse)request.GetResponse())

我的方法在这里是静态的原因是因为我只是在另一个项目中运行它来测试它。我的问题是,为什么它只会在第二个文件上死掉?我一直盯着自己瞎了好几个小时!

2 个答案:

答案 0 :(得分:3)

我也遇到了这个问题...尝试先完成请求然后关闭它,然后再尝试检索响应。这对我有用(实际上在阅读MartinNielsen的评论后尝试过)。这就是我所做的。

        // 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;

以下是我在编写此代码时使用的一些资源(不会让我发布超过2个,还有更多)

How to: Upload Files with FTP

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

答案 1 :(得分:0)

我不是C#的专家,但我使用此代码从我的ftp下载文件:

public void Download(string filename)
    {
        // I try to download five times before crash
        for (int i = 1; i < 5; i++)
        {
            try
            {
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(Global.Path + "/" + filename);
                ftp.Credentials = new NetworkCredential(User, Pass);
                ftp.KeepAlive = false;
                ftp.Method = WebRequestMethods.Ftp.DownloadFile;
                ftp.UseBinary = true;
                ftp.Proxy = null;

                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                string LocalDirectory = Application.StartupPath.ToString() + "/downloads/" + filename;
                using (FileStream fs = new FileStream(LocalDirectory, FileMode.Create, FileAccess.Write, FileShare.None))
                using (Stream strm = ftp.GetResponse().GetResponseStream())
                {
                    contentLen = strm.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        fs.Write(buff, 0, contentLen);
                        contentLen = strm.Read(buff, 0, buffLength);
                    }
                }

                Process.Start(LocalDirectory);
                break;
            }
            catch (Exception exc)
            {
                if (i == 5)
                {
                    MessageBox.Show("Can't download, try number: " + i + "/5 \n\n Error: " + exc.Message,
                        "Problem downloading the file");
                }
            }
        }
    }

告诉我它是否适合你:)