Bon*_*nie 5 vb.net ftp download ftpwebrequest ftpwebresponse
我们需要使用vb.net从远程FTP服务器获取大约100个非常小的文件.我们公司不会让我们购买(或安装)任何第三方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
我刚刚放在一起的东西,重要的部分是 fwr.Proxy = Nothing,否则它会尝试自动获取代理设置,这会导致巨大的延迟,因此将其设置为无会强制它不使用代理设置。
如果您使用代理,显然您需要将其设置为实际代理。
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress)
fwr.Credentials = New NetworkCredential(userName, password)
fwr.KeepAlive = True
fwr.Method = WebRequestMethods.Ftp.ListDirectory
fwr.Proxy = Nothing
Try
Dim sr As New IO.StreamReader(fwr.GetResponse().GetResponseStream())
Dim lst = sr.ReadToEnd().Split(vbNewLine)
For Each file As String In lst
file = file.Trim() 'remove any whitespace
If file = ".." OrElse file = "." Then Continue For
Dim fwr2 As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress & file)
fwr2.Credentials = fwr.Credentials
fwr2.KeepAlive = True
fwr2.Method = WebRequestMethods.Ftp.DownloadFile
fwr2.Proxy = Nothing
Dim fileSR As New IO.StreamReader(fwr2.GetResponse().GetResponseStream())
Dim fileData = fileSR.ReadToEnd()
fileSR.Close()
Next
sr.Close()
Catch ex As Exception
End Try
Run Code Online (Sandbox Code Playgroud)
我知道有点晚了但希望有帮助
| 归档时间: |
|
| 查看次数: |
29329 次 |
| 最近记录: |