Tuesday, June 10, 2008

Sample code for FTP client using System.net (FTPWebRequest)


Imports System.Text
Imports System.Net
Imports System.IO

Public Class FTPClient
Private UserID As String
Private Password As String

Public Sub New(ByVal UserID As String, ByVal Password As String)
Me.UserID = UserID
Me.Password = Password
End Sub

Public Sub uploadFile(ByVal URI As String, ByVal UploadFileName As
String, ByVal LocalPath As String, ByVal LocalFileName As String, Optional
ByVal FTPUserID As String = "", Optional ByVal FTPPassword As String = "")
Dim completePath = LocalPath + "/" + LocalFileName
Dim fileInf As FileInfo = New FileInfo(completePath)

If UploadFileName = "" Then
UploadFileName = LocalFileName
End If

If FTPUserID = "" Then
FTPUserID = Me.UserID
End If

If FTPPassword = "" Then
FTPPassword = Me.Password
End If

Dim MyURI As String = URI + "/" + UploadFileName



Dim reqFTP As FtpWebRequest


Dim buffLength As Integer = 2048
Dim buff(buffLength) As Byte
Dim contentLen As Integer

Dim response As FtpWebResponse = Nothing

Dim fs As FileStream = fileInf.OpenRead()
Dim strm As Stream = Nothing

Try
reqFTP = CType(FtpWebRequest.Create(New Uri(MyURI)),
FtpWebRequest)
reqFTP.Credentials = New NetworkCredential(FTPUserID,
FTPPassword)
reqFTP.KeepAlive = False
reqFTP.Method = WebRequestMethods.Ftp.UploadFile
reqFTP.UseBinary = True
reqFTP.ContentLength = fileInf.Length
reqFTP.UsePassive = True

response = CType(reqFTP.GetResponse(), FtpWebResponse)

strm = reqFTP.GetRequestStream()
contentLen = fs.Read(buff, 0, buffLength)

While (contentLen <> 0)
strm.Write(buff, 0, contentLen)
contentLen = fs.Read(buff, 0, buffLength)
End While
Finally
strm.Close()
fs.Close()
response.Close()
End Try
End Sub


Public Function GetFileList(ByVal URI As String, Optional ByVal
FTPUserID As String = "", Optional ByVal FTPPassword As String = "") As
String()
Dim downloadFiles() As String
Dim result As StringBuilder = New StringBuilder()
Dim reqFTP As FtpWebRequest = Nothing
Dim response As WebResponse = Nothing
Dim reader As StreamReader = Nothing

If FTPUserID = "" Then
FTPUserID = Me.UserID
End If

If FTPPassword = "" Then
FTPPassword = Me.Password
End If

Try
reqFTP = CType(FtpWebRequest.Create(URI), FtpWebRequest)
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPUserID,
FTPPassword)
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory

response = reqFTP.GetResponse()

reader = New StreamReader(response.GetResponseStream())

Dim line As String = reader.ReadLine()

While Not line Is Nothing
result.Append(line)
result.Append("\n")
line = reader.ReadLine()
End While

result.Remove(result.ToString().LastIndexOf("\n"), 1)

downloadFiles = result.ToString().Split("\n")
Catch ex As Exception
downloadFiles = Nothing
Finally
reader.Close()
response.Close()
End Try

Return downloadFiles
End Function


Public Sub downloadFile(ByVal Uri As String, ByVal ToDownLoadFileName
As String, ByVal LocalPath As String, Optional ByVal LocalFileName As
String = "", Optional ByVal FTPUserID As String = "", Optional ByVal
FTPPassword As String = "")
Dim result As StringBuilder = New StringBuilder()
Dim reqFTP As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing
Dim reader As StreamReader = Nothing

Dim ftpStream As Stream = Nothing
Dim outputStream As FileStream = Nothing

If FTPUserID = "" Then
FTPUserID = Me.UserID
End If

If FTPPassword = "" Then
FTPPassword = Me.Password
End If

If LocalFileName = "" Then
LocalFileName = ToDownLoadFileName
End If

Try
outputStream = New FileStream(LocalPath + "\\" + LocalFileName,
FileMode.Create)
reqFTP = CType(FtpWebRequest.Create(Uri + "/" +
ToDownLoadFileName), FtpWebRequest)
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPUserID,
FTPPassword)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile


response = CType(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()

Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048

Dim readCount As Integer
Dim buffer(bufferSize) As Byte
readCount = ftpStream.Read(buffer, 0, bufferSize)

While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
Finally
ftpStream.Close()
outputStream.Close()
response.Close()
End Try
End Sub
End Class

No comments: