FTP EM VB.NET
Bom dia pessoas.
Estou tendo um problema em criar um cliente FTP.
o erro é:
Ocorreu uma exceção sem tratamento do tipo [ô]System.Net.WebException[ô] em System.dll
Informações adicionais: O servidor remoto retornou um erro: (404) Não Localizado.
Porém a pasta existe e a uso diariamente via FileZilla.
Já estudei os 3 fontes que tem aqui no VBMania e os diversos exemplos na MSDN.
A ideia é listar os arquivos e com base em uma tabela (log_ja_baixados), baixar os faltantes.
Para listar estou usando este código em um Modulo:
[txt-color=#0000f0] Public Function[/txt-color] GetFileList([txt-color=#0000f0]ByVal [/txt-color]host [txt-color=#0000f0]As String[/txt-color], [txt-color=#0000f0]ByVal [/txt-color]username [txt-color=#0000f0]As String[/txt-color], [txt-color=#0000f0]ByVal [/txt-color]password [txt-color=#0000f0]As String[/txt-color], [txt-color=#0000f0]ByVal [/txt-color]currentdirectory [txt-color=#0000f0]As String[/txt-color]) As List([txt-color=#0000f0]Of String[/txt-color])
[txt-color=#0000f0]Dim [/txt-color]oFTP [txt-color=#0000f0]As [/txt-color]FtpWebRequest = [txt-color=#0000f0]CType[/txt-color](FtpWebRequest.Create(host & currentdirectory), FtpWebRequest)
oFTP.Credentials = [txt-color=#0000f0]New [/txt-color]NetworkCredential(username, password)
oFTP.KeepAlive = [txt-color=#0000f0]True[/txt-color]
oFTP.Method = WebRequestMethods.Ftp.ListDirectory
[txt-color=#0000f0]Dim [/txt-color]response [txt-color=#0000f0]As [/txt-color]FtpWebResponse = [txt-color=#0000f0]CType[/txt-color](oFTP.GetResponse, FtpWebResponse)
Dim sr As StreamReader = New StreamReader(response.GetResponseStream)
[txt-color=#0000f0]Dim [/txt-color]str[txt-color=#0000f0] As String[/txt-color] = sr.ReadLine
[txt-color=#0000f0]Dim [/txt-color]oList[txt-color=#0000f0] As New[/txt-color] List[txt-color=#0000f0](Of String[/txt-color])
[txt-color=#0000f0]While [/txt-color]str[txt-color=#0000f0] IsNot Nothing[/txt-color]
[txt-color=#0000f0]If [/txt-color] Len(str) > 0 [txt-color=#0000f0]Then[/txt-color]
oList.Add(str)
[txt-color=#0000f0] End If[/txt-color]
str = sr.ReadLine
[txt-color=#0000f0]End While[/txt-color]
sr.Close()
response.Close()
oFTP = [txt-color=#0000f0]Nothing[/txt-color]
[txt-color=#0000f0]Return [/txt-color]oList
Form1.List1.Items.Add(oList)
[txt-color=#0000f0] End Function[/txt-color]
Ainda vou colocar a referencia da tabela.
Estou tendo um problema em criar um cliente FTP.
o erro é:
Ocorreu uma exceção sem tratamento do tipo [ô]System.Net.WebException[ô] em System.dll
Informações adicionais: O servidor remoto retornou um erro: (404) Não Localizado.
Porém a pasta existe e a uso diariamente via FileZilla.
Já estudei os 3 fontes que tem aqui no VBMania e os diversos exemplos na MSDN.
A ideia é listar os arquivos e com base em uma tabela (log_ja_baixados), baixar os faltantes.
Para listar estou usando este código em um Modulo:
[txt-color=#0000f0] Public Function[/txt-color] GetFileList([txt-color=#0000f0]ByVal [/txt-color]host [txt-color=#0000f0]As String[/txt-color], [txt-color=#0000f0]ByVal [/txt-color]username [txt-color=#0000f0]As String[/txt-color], [txt-color=#0000f0]ByVal [/txt-color]password [txt-color=#0000f0]As String[/txt-color], [txt-color=#0000f0]ByVal [/txt-color]currentdirectory [txt-color=#0000f0]As String[/txt-color]) As List([txt-color=#0000f0]Of String[/txt-color])
[txt-color=#0000f0]Dim [/txt-color]oFTP [txt-color=#0000f0]As [/txt-color]FtpWebRequest = [txt-color=#0000f0]CType[/txt-color](FtpWebRequest.Create(host & currentdirectory), FtpWebRequest)
oFTP.Credentials = [txt-color=#0000f0]New [/txt-color]NetworkCredential(username, password)
oFTP.KeepAlive = [txt-color=#0000f0]True[/txt-color]
oFTP.Method = WebRequestMethods.Ftp.ListDirectory
[txt-color=#0000f0]Dim [/txt-color]response [txt-color=#0000f0]As [/txt-color]FtpWebResponse = [txt-color=#0000f0]CType[/txt-color](oFTP.GetResponse, FtpWebResponse)
Dim sr As StreamReader = New StreamReader(response.GetResponseStream)
[txt-color=#0000f0]Dim [/txt-color]str[txt-color=#0000f0] As String[/txt-color] = sr.ReadLine
[txt-color=#0000f0]Dim [/txt-color]oList[txt-color=#0000f0] As New[/txt-color] List[txt-color=#0000f0](Of String[/txt-color])
[txt-color=#0000f0]While [/txt-color]str[txt-color=#0000f0] IsNot Nothing[/txt-color]
[txt-color=#0000f0]If [/txt-color] Len(str) > 0 [txt-color=#0000f0]Then[/txt-color]
oList.Add(str)
[txt-color=#0000f0] End If[/txt-color]
str = sr.ReadLine
[txt-color=#0000f0]End While[/txt-color]
sr.Close()
response.Close()
oFTP = [txt-color=#0000f0]Nothing[/txt-color]
[txt-color=#0000f0]Return [/txt-color]oList
Form1.List1.Items.Add(oList)
[txt-color=#0000f0] End Function[/txt-color]
Ainda vou colocar a referencia da tabela.
Fiz algo parecido uma vez mas já faz tempo....
Tenta assim:
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
Tenta assim:
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
Obrigado Alevale.
Consegui usar.
Consegui usar.
Tópico encerrado , respostas não são mais permitidas