简体   繁体   English

FTP仅在使用Thread.Sleep时有效

[英]FTP only works when I use Thread.Sleep

I have a small console app that scrapes some data and uploads it to an FTP directory using the FtpWebRequest object - very typical. 我有一个小型控制台应用程序,该应用程序会抓取一些数据,并使用FtpWebRequest对象将其上传到FTP目录-非常典型。

I had it working successfully while I was stepping through it with the debugger. 在调试器中逐步调试时,它已成功运行。 Files were being written to the remote server with the correct data. 正在使用正确的数据将文件写入远程服务器。 Then when I took the breakpoints out and let it rip, it created the file with no data, then hung until it finally timed out. 然后,当我取出断点并将其撕裂时,它创建了没有数据的文件,然后挂起直到最终超时。 No exception other than the timeout was returned. 除了超时没有返回任何异常。 I put the breakpoint back in and sure enough it worked perfectly. 我将断点放回去,并确定它可以正常工作。

I figured that the act of debugging was causing a pause and put in a Thread.Sleep(10), and, again, worked perfectly. 我认为调试的行为导致了暂停,并放入Thread.Sleep(10),并且再次运行良好。

I'd be happy with just an explanation for this (as long as it's not terrible practice), but suggestions on how to do it better would also be appreciated. 只要对此做一个解释,我会很高兴的(只要这不是很糟糕的做法),但是关于如何做得更好的建议也将不胜感激。 I'll note that I only sparingly use 3rd Party controls. 我会注意,我仅少量使用3rd Party控件。

EDIT: I should also mention that I don't want to have to create a physical file, I'd rather just write the Stream directly. 编辑:我还应该提到,我不想创建一个物理文件,而是直接编写Stream。

Here's the relevant code: 以下是相关代码:

        filePathAndName = new Uri(String.Format("{0}{1}_{2}.txt", Properties.Settings.Default.QTMFTPDirectory, 
            fileNamePrefix, DateTime.Now.ToString("MMddyyHHmmss")));
        fileContents = Encoding.UTF8.GetBytes(csv);

        request = (FtpWebRequest)WebRequest.Create(filePathAndName);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(Properties.Settings.Default.FTPUserName, Properties.Settings.Default.FTPPassword);
        request.ContentLength = fileContents.Length;
        request.KeepAlive = false;

        try
        {
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(fileContents, 0, fileContents.Length);
                System.Threading.Thread.Sleep(10); // Won't work without this.
            }
        }
        catch (Exception ex)
        {
            LogError(ex);
        }

Thanks! 谢谢!

Try manually closing it. 尝试手动关闭它。

reqStream.Close(); // Close

and if that does not help, you can try getting the response from the server. 并且如果这样做没有帮助,您可以尝试从服务器获取响应。

GetResponse(); 

Source: 资源:

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.getrequeststream(v=vs.110).aspx http://msdn.microsoft.com/zh-cn/library/system.net.ftpwebrequest.getrequeststream(v=vs.110).aspx

Edit: 编辑:

I know, the using keyword sometimes does not close a connection correctly. 我知道, using关键字有时无法正确关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM