使用 FTP 下载每个文件*同时*获取文件列表
我们需要使用 vb.net 从远程 FTP 服务器获取大约 100 个非常小的文件。 我们公司不允许我们购买(或安装)任何第 3 方 ftp 库...所以我们被迫使用 FtpWebRequest 之类的东西。 (或者是否有更好的免费选择,它已经是 Visual Studio 的一部分?)
此方法有效,但速度非常慢。 (我认为是因为不断地登录/退出。)
Log in with user name and password. Get a file-list from the remote server. Log out Use that file-list to get each file separtely: Log in, get the file, log out. Log in 99 more times, get each file, log out each time.
相反,我们可能应该这样做,但它永远不会起作用:
Log in with user name and password. ONCE. Get a list of filenames. Download each file. Log out ONCE.
我们在网上找到了无数“获取 FTP 文件列表”和后来的“如何使用 FTP 下载 1 个文件”的示例“......但我们从未看到“获取每个文件名,然后立即下载”。
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpSite) fwr.Credentials = New NetworkCredential(userName, password) fwr.KeepAlive = True fwr.Method = WebRequestMethods.Ftp.ListDirectory Dim sr As IO.StreamReader = Nothing Try sr = New IO.StreamReader(fwr.GetResponse().GetResponseStream()) Do Until (sr.EndOfStream()) fileName = sr.ReadLine() fwr.Method = WebRequestMethods.Ftp.DownloadFile output = "" Dim sr2 As IO.StreamReader = Nothing Try sr2 = New IO.StreamReader(fwr.GetResponse().GetResponseStream()) output = sr2.ReadToEnd() Catch ex As Exception End Try If (Not sr2 Is Nothing) Then sr2.Close() : sr2 = Nothing Call MsgBox("Got " & fileName & LF & output) Loop Catch ex As Exception End Try If (Not sr Is Nothing) Then sr.Close() : sr = Nothing If (Not fwr Is Nothing) Then fwr = Nothing
We need to get about 100 very small files from a remote FTP server using vb.net.
Our company won't let us buy (or install) any 3rd party ftp libraries... so we are forced to use something like FtpWebRequest. (Or is there a better free, choice that is already a part of Visual Studio?)
This method works, but it is VERY slow. (I assume because of the constant logging in/out.)
Log in with user name and password. Get a file-list from the remote server. Log out Use that file-list to get each file separtely: Log in, get the file, log out. Log in 99 more times, get each file, log out each time.
Instead, we probably should be doing this, but it never works:
Log in with user name and password. ONCE. Get a list of filenames. Download each file. Log out ONCE.
We found COUNTLESS examples online of "getting an FTP file list" and later "how to download 1 file with FTP"... but we never see "get EACH file name, and download it NOW".
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpSite) fwr.Credentials = New NetworkCredential(userName, password) fwr.KeepAlive = True fwr.Method = WebRequestMethods.Ftp.ListDirectory Dim sr As IO.StreamReader = Nothing Try sr = New IO.StreamReader(fwr.GetResponse().GetResponseStream()) Do Until (sr.EndOfStream()) fileName = sr.ReadLine() fwr.Method = WebRequestMethods.Ftp.DownloadFile output = "" Dim sr2 As IO.StreamReader = Nothing Try sr2 = New IO.StreamReader(fwr.GetResponse().GetResponseStream()) output = sr2.ReadToEnd() Catch ex As Exception End Try If (Not sr2 Is Nothing) Then sr2.Close() : sr2 = Nothing Call MsgBox("Got " & fileName & LF & output) Loop Catch ex As Exception End Try If (Not sr Is Nothing) Then sr.Close() : sr = Nothing If (Not fwr Is Nothing) Then fwr = Nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看我的 FTP 课程,它可能正是您所需要的。
初始化:
Take a look at my FTP class, it might be exactly what you need.
To initialize:
我刚刚放在一起的东西,重要的部分是 fwr.Proxy = Nothing,否则它会尝试自动获取代理设置,这会导致巨大的延迟,因此将其设置为无会强制它不使用代理设置。
如果您使用代理,显然您需要将其设置为实际代理。
我知道有点晚了但希望有帮助
Something I just put together, the important part is the fwr.Proxy = Nothing, otherwise it tries to auto get the proxy settings which causes huge delays so setting it to nothing forces it to not use one.
If you are using a proxy obviously you need to set this to an actual proxy.
I know its a bit late but hopefully helps