首先,对于这个微不足道的问题我很抱歉,我是新手,还在学习。我正在使用vb.net构建一个连接到FTP服务器列表文件的应用程序,并根据文件扩展名过滤这些文件。然后下载并删除该文件。
我收到错误:"底层连接已关闭:服务器违反了协议#34;
这是我调用方法的地方。
Call GetDirectoryListing(strExtension, strPastaDownload, strUser, strPsswd)
strFileNumber = Cont_Files
Dim blnDownloaded As Boolean = False
If Cont_Files > 0 Then
For cont = 0 To Cont_Files - 1
nameFile = arrArq(cont)
strFilename = UCase(nameFile)
strLocal = AppPath & strLocalArquivos & "\" & nameFile
blnDownloaded = mFTP.FtpDownloadFile(strPastaDownload, strNomedoArquivo, strLocal, strUsuario, strSenha)
StrDir = Dir(strdirectory)
strRemote = nameFile
If StrDir <> "" Then
FileCopy(AppPath & strfiledirectory & "\" & Trim(strRemote), strLocalBackup & "\" & Trim(strRemote))
mFTP.FtpDeleteFile(strServer, strPastaFtp, strFilename, strUsuario, strSenha)
End If
Next
这里列出文件的功能我在这里没有任何错误只是为了说明
Public Function GetDirectoryListing(ByVal sFilter As String, ByVal strServer As String, ByVal strLogin As String, ByVal strPassword As String) As ArrayList
Dim ftpRequest As FtpWebRequest = Nothing
Try
ftpRequest = FtpWebRequest.Create(New Uri(strServer))
With ftpRequest
.Credentials = New NetworkCredential(strLogin, strPassword)
.UsePassive = True
.Method = WebRequestMethods.Ftp.ListDirectory
.KeepAlive = True
End With
'ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory 'List, not Download
'Read the contents of the directory and store it into a string array
Dim srReader As New StreamReader(ftpRequest.GetResponse().GetResponseStream())
sDirectoryList = srReader.ReadToEnd().Split(vbNewLine)
For i As Integer = 0 To UBound(sDirectoryList) - 1
If sFilter = Mid(sDirectoryList(i), Len(sDirectoryList(i)) - 2, 3) Then
listaArquivo.Add(sDirectoryList(i))
End If
Next
'Clear the last split vbNewLine
If sDirectoryList IsNot Nothing Then
sDirectoryList(sDirectoryList.Length - 1) = ""
End If
srReader.Close()
srReader = Nothing
GetDirectoryListing = listaArquivo
ftpRequest = Nothing
Catch ex As WebException
MessageBox.Show("Failed to get directory listing. GetDirectoryListing()")
'MessageBox.Show(ex.ToString())
End Try
End Function
以下是下载功能
Public Function FtpDownloadFile(ByVal strUrl As String, ByVal strFileName As String, ByVal strTargetDirectory As String, ByVal strLogin As String, ByVal strPassword As String) As Boolean
Dim ftpRequest As FtpWebRequest = Nothing
Dim ftpResponse As FtpWebResponse = Nothing
Dim strFtpFile As String = ""
Dim strTargetFile As String = ""
Dim strmReader As StreamReader = Nothing
Dim strmResponse As Stream = Nothing
Dim strmTarget As Stream = Nothing
Dim strmWriter As StreamWriter = Nothing
strFtpFile = strUrl & "/" & Trim(strFileName)
strTargetFile = strTargetDirectory
Try
ftpRequest = CType(WebRequest.Create(strFtpFile), FtpWebRequest)
With ftpRequest
.Credentials = New NetworkCredential(strLogin, strPassword)
.UsePassive = True
.Method = WebRequestMethods.Ftp.DownloadFile
.KeepAlive = True
End With
ftpResponse = CType(ftpRequest.GetResponse(), FtpWebResponse) 'line where I am getting the error
strmTarget = New FileStream(strTargetFile, FileMode.Create)
strmWriter = New StreamWriter(strmTarget)
strmResponse = ftpResponse.GetResponseStream()
strmReader = New StreamReader(strmResponse, System.Text.Encoding.UTF8)
strmWriter.Write(strmReader.ReadToEnd())
Catch ex As Exception
Throw ex
Finally
If strmReader IsNot Nothing Then
strmReader.Close()
strmReader.Dispose()
End If
If strmWriter IsNot Nothing Then
strmWriter.Close()
strmWriter.Dispose()
End If
If ftpResponse IsNot Nothing Then
ftpResponse.Close()
ftpResponse = Nothing
End If
If ftpRequest IsNot Nothing Then ftpRequest = Nothing
End Try
Return True
End Function
提前感谢您的帮助
答案 0 :(得分:0)
我认为问题出在FtpDownloadFile
方法的第WebRequest
行中,将FtpWebRequest
转换为ftpRequest = FtpWebRequest.Create(New Uri(strServer))
ftpRequest = CType(WebRequest.Create(strFtpFile), FtpWebRequest)
。它们不一样。
{{1}}
尝试确保每次都以相同的方式连接到服务器。使用有效的方式。