Dim myFtpWebRequest As FtpWebRequest = CType(FtpWebRequest.Create(target),FtpWebRequest)参数target为FTP配置的网络资源URI。同前面HttpWebRequest一样,FtpWebRequest.Create(target)返回类型是WebRequest,所以需要转类型为FtpWebRequest。
Using myResponse As FtpWebResponse =CType(myFtpWebRequest.GetResponse, FtpWebResponse)
1、文件操作
AppendFile FTP APPE通信协议方法,用来将文件附加到FTP服务器上的现有文件myFtpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile
Public Overrides Property Credentials As ICredentials需指定一个实际应用ICredentials接口的对象,用来封装账号与密码等相关信息。
Dim myNetworkCredential = New NetworkCredential(username, uaerpwd) myFtpWebRequest.Credentials = myNetworkCredential
Imports System.Net Public Class Form1 Private Sub txtOpera_Click(sender As Object, e As EventArgs) Handles txtOpera.Click DirOperation() End Sub Private Sub DirOperation() Try Dim frq As FtpWebRequest = CType(FtpWebRequest.Create(txtFTP.Text), FtpWebRequest) Dim fcr As New NetworkCredential(txtUser.Text, txtPassword.Text) Dim frp As FtpWebResponse frq.Credentials = fcr '认证信息 Select Case True Case rdoMake.Checked frq.Method = WebRequestMethods.Ftp.MakeDirectory Case rdoDele.Checked frq.Method = WebRequestMethods.Ftp.RemoveDirectory Case rdoReName.Checked If txtNewFTP.Text <> "" Then frq.Method = WebRequestMethods.Ftp.Rename frq.RenameTo = txtNewFTP.Text End If Case Else MsgBox("未选择!") End Select frp = CType(frq.GetResponse, FtpWebResponse) '发送、操作、并返回 frp.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class
Dim myStream As Stream = myResponse.GetResponseStream()取得服务器FtpWebResponse.GetResponseStream()返回的数据流,对该流进行读写即可得目录列举信息。
Imports System.Net Imports System.IO Imports System.Text Public Class Form1 Private Sub btnFtpList_Click(sender As Object, e As EventArgs) Handles btnFtpList.Click Try Dim frq As FtpWebRequest = CType(FtpWebRequest.Create(txtFTP.Text), FtpWebRequest) Dim cre As New NetworkCredential(txtUser.Text, txtPassword.Text) Dim frp As FtpWebResponse Dim st As Stream Dim sr As StreamReader frq.Credentials = cre If rdoSimple.Checked = True Then frq.Method = WebRequestMethods.Ftp.ListDirectory Else frq.Method = WebRequestMethods.Ftp.ListDirectoryDetails End If frp = CType(frq.GetResponse, FtpWebResponse) st = frp.GetResponseStream sr = New StreamReader(st, Encoding.UTF8) '对响应流读取 txtListDrectory.Text = sr.ReadToEnd frp.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class
Imports System.Net Imports System.IO Public Class Form1 Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential Dim st As Stream, fs As FileStream Dim byt(1024) As Byte, intCount As Integer Try frq = CType(WebRequest.Create(New Uri(txtFtp.Text)), FtpWebRequest) fcr = New NetworkCredential(txtUser.Text, txtPassword.Text) frq.Credentials = fcr frq.Method = WebRequestMethods.Ftp.DownloadFile frq.UseBinary = True frp = CType(frq.GetResponse, FtpWebResponse) st = frp.GetResponseStream fs = New FileStream(txtSave.Text, FileMode.Create) Do intCount = st.Read(byt, 0, 1024) fs.Write(byt, 0, intCount) Loop While intCount > 0 fs.Close() frp.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class
Public Overrides Function GetRequestStream As Stream
此方法返回一个Stream数据流对象,写入此数据流的数据内容,进一步被送到要求链接的服务器,完成上传操作。
Imports System.Net Imports System.IO Public Class Form1 Private Sub btnFtp_Click(sender As Object, e As EventArgs) Handles btnFtp.Click Dim frq As FtpWebRequest, frp As FtpWebResponse Dim st As Stream, fs As FileStream Dim byt(1024) As Byte, intCount As Integer frq = CType(WebRequest.Create(txtFile.Text), FtpWebRequest) frq.Credentials = New NetworkCredential(txtUser.Text, txtPassword.Text) frq.Method = WebRequestMethods.Ftp.UploadFileWithUniqueName st = frq.GetRequestStream fs = New FileStream(txtFile.Text, FileMode.Open, FileAccess.Read) Do intCount = fs.Read(byt, 0, 1024) '将文件流加载到请求流上 st.Write(byt, 0, intCount) Loop While (intCount > 0) frp = CType(frq.GetResponse, FtpWebResponse) frp.Close() MsgBox("完成!") End Sub End Class
Public Sub DownloadFile ( address As Uri, fileName As String) Public Function UploadFile ( address As Uri, fileName As String )As Byte