程序挂在 FtpWebResponse 上

发布于 2024-11-13 09:41:47 字数 1569 浏览 3 评论 0原文

第一次发帖,长期读者。我有一个非常烦人的问题一直困扰着我。我已经设置了一个程序,这样我就可以在 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())

我的方法是静态的原因是因为我只是在不同的项目中运行它来测试它。我的问题是,它为什么只在第二个就死掉文件?我已经盲目地盯着自己好几个小时了!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-11-20 09:41:47

我也遇到了这个问题...尝试先完成您的请求,然后在尝试检索响应之前关闭它。这对我有用(实际上在阅读 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 个,还有更多)

如何:使用 FTP 上传文件

上传文件--“请求的 URI 对于该 FTP 命令无效”

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"

寻梦旅人 2024-11-20 09:41:47

我不是 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");
                }
            }
        }
    }

告诉我它是否适合您:)

I'm not expert on C# but I use this code to download files from my 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");
                }
            }
        }
    }

Tell me if it works for you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文