VB.NET连接FTP

yunyun837 2008-05-20 05:11:49
我用下面的方法连接FTP,想把上面的jilu.dt文件下到本地的d盘上面,但是总也连不上,但是如果换一个就能连上,问一下,FTP分好几种么?之间的连接方法都不相同么?


Private Sub cdl1()
Try
If IO.File.Exists("d:/Fx1kntai.dt") Then
IO.File.Delete("d:/Fx1kntai.dt")
End If
My.Computer.Network.DownloadFile("ftp://vpn.li-kkk.com/jilu.dt", "d:/jilu.dt", "admin", "admin")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


抛出的异常是:WebException
...全文
409 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yunyun837 2008-08-27
  • 打赏
  • 举报
回复
谢谢大家~~~~~~~~~~~~~~~~~
downmoon 2008-07-18
  • 打赏
  • 举报
回复
参考

http://blog.csdn.net/downmoon/archive/2008/01/29/2071776.aspx
http://www.cnblogs.com/downmoon/archive/2008/01/29/1057726.html
gyc 2008-07-18
  • 打赏
  • 举报
回复
你先用IE 试试你那个地址是否有效

另外, 那个文件路径用的是 d:\abc.dat
jyorei 2008-07-18
  • 打赏
  • 举报
回复
#Region "获取FTP服务器中指定目录的所有文件名"
'Return a list of files in a string() array from the file system.
Public Function GetFileList(ByVal sMask As String) As String()
Dim cSocket As Socket
Dim bytes As Int32
Dim seperator As Char = ControlChars.Lf
Dim mess() As String
Dim i As Integer = 0
m_sMes = ""
'Check if you are logged on to the FTP server.
If (Not (m_bLoggedIn)) Then
Login()
End If

cSocket = CreateDataSocket()
'Send an FTP command,
SendCommand("NLST " & sMask)

If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
'output.Close()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If

m_sMes = ""
Do While (True)
m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)

If (bytes < m_aBuffer.Length) Then
Exit Do
End If
Loop

mess = m_sMes.Split(seperator)
For i = 0 To mess.Length - 1
If mess(i) <> "" Then
mess(i) = Microsoft.VisualBasic.Left(mess(i), Len(mess(i)) - 1)
End If
Next i
cSocket.Close()
ReadReply()

If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If

Return mess
End Function
#End Region

#Region "取得文件大小"
' Get the size of the file on the FTP server.
Public Function GetFileSize(ByVal sFileName As String) As Long
Dim size As Long

If (Not (m_bLoggedIn)) Then
Login()
End If
'Send an FTP command.
SendCommand("SIZE " & sFileName)
size = 0

If (m_iRetValue = 213) Then
size = Int64.Parse(m_sReply.Substring(4))
Else
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If

Return size
End Function
#End Region

#Region "FTP登录联结Log on"
'Log on to the FTP server.
Public Function Login() As Boolean

m_objClientSocket = _
New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

Dim ep As New IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList(0), m_iRemotePort)

Try
m_objClientSocket.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to remote server")

End Try

ReadReply()
If (m_iRetValue <> 220) Then
CloseConnection()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
'Send an FTP command to send a user logon ID to the server.
SendCommand("USER " & m_sRemoteUser)
If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then
Cleanup()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If

If (m_iRetValue <> 230) Then
'Send an FTP command to send a user logon password to the server.
SendCommand("PASS " & m_sRemotePassword)
If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then
Cleanup()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
End If

m_bLoggedIn = True
'Call the ChangeDirectory user-defined function to change the folder to the
'remote FTP folder that is mapped.
ChangeDirectory(m_sRemotePath)

'Return the final result.
Return m_bLoggedIn
End Function
#End Region

#Region "BinaryMode"
'If the value of mode is true, set the binary mode for downloads. Otherwise, set ASCII mode.
Public Sub SetBinaryMode(ByVal bMode As Boolean)

If (bMode) Then
'Send the FTP command to set the binary mode.
'(TYPE is an FTP command that is used to specify representation type.)
SendCommand("TYPE I")
Else
'Send the FTP command to set ASCII mode.
'(TYPE is a FTP command that is used to specify representation type.)
SendCommand("TYPE A")
End If

If (m_iRetValue <> 200) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
End Sub
#End Region
jyorei 2008-07-18
  • 打赏
  • 举报
回复
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Net.Sockets

Public Class FTPAccess

#Region "Class Variable Declarations"
Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String
Private m_sRemotePassword, m_sMess As String
Private m_iRemotePort, m_iBytes As Int32
Private m_objClientSocket As Socket

Private m_iRetValue As Int32
Public m_bLoggedIn As Boolean
Private m_sMes, m_sReply As String

'Set the size of the packet that is used to read and to write data to the FTP server
'to the following specified size.
Public Const BLOCK_SIZE = 512
Private m_aBuffer(BLOCK_SIZE) As Byte
Private ASCII As Encoding = Encoding.ASCII
Public flag_bool As Boolean
'General variable declaration
Private m_sMessageString As String
#End Region

#Region "Class Constructors"

' Main class constructor
Public Sub New()

m_sRemoteHost = "192.168.0.121"
m_sRemotePath = "."
m_sRemoteUser = "user"
m_sRemotePassword = "pw"
m_sMessageString = ""
m_iRemotePort = 3721
m_bLoggedIn = False
End Sub

' Parameterized constructor
Public Sub New(ByVal sRemoteHost As String, _
ByVal sRemotePath As String, _
ByVal sRemoteUser As String, _
ByVal sRemotePassword As String, _
ByVal iRemotePort As Int32)
m_sRemoteHost = sRemoteHost
m_sRemotePath = sRemotePath
m_sRemoteUser = sRemoteUser
m_sRemotePassword = sRemotePassword
m_sMessageString = ""
m_iRemotePort = 21
m_bLoggedIn = False
End Sub
#End Region

#Region "FTP连接相关设置 "

'Set or Get the name of the FTP server that you want to connect.
Public Property RemoteHostFTPServer() As String
'Get the name of the FTP server.
Get
Return m_sRemoteHost
End Get
'Set the name of the FTP server.
Set(ByVal Value As String)
m_sRemoteHost = Value
End Set
End Property

'Set or Get the FTP Port Number of the FTP server that you want to connect.
Public Property RemotePort() As Int32
'Get the FTP Port Number.
Get
Return m_iRemotePort
End Get
'Set the FTP Port Number.
Set(ByVal Value As Int32)
m_iRemotePort = Value

End Set
End Property

'Set or Get the remote path of the FTP server that you want to connect.
Public Property RemotePath() As String
'Get the remote path.
Get
Return m_sRemotePath
End Get
'Set the remote path.
Set(ByVal Value As String)
m_sRemotePath = Value
End Set
End Property

'Set or Get the remote password of the FTP server that you want to connect.
Public Property RemotePassword() As String
Get
Return m_sRemotePassword
End Get
Set(ByVal Value As String)
m_sRemotePassword = Value
End Set
End Property

'Set or Get the remote user of the FTP server that you want to connect.
Public Property RemoteUser() As String
Get
Return m_sRemoteUser
End Get
Set(ByVal Value As String)
m_sRemoteUser = Value
End Set
End Property

'Set the class MessageString.
Public Property MessageString() As String
Get
Return m_sMessageString
End Get
Set(ByVal Value As String)
m_sMessageString = Value
End Set
End Property

#End Region

#Region "Public Subs and Functions"
jyorei 2008-07-18
  • 打赏
  • 举报
回复
稍等给你一个好用的类,一次发不下
it_gz_xi 2008-05-21
  • 打赏
  • 举报
回复
个人感觉用那种方法对FTP的操作功能多一些,调试时候比你用第一种更好知道是哪里出错
yunyun837 2008-05-21
  • 打赏
  • 举报
回复
你说的使这种么?

Private Sub cdl()
Try
Dim uri As New Uri("ftp://vpn.li-kkk.com/jilu.dt")
Dim request As FtpWebRequest = CType(WebRequest.Create(uri), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile

request.Credentials = New NetworkCredential("admin", "admin")

Dim response As FtpWebResponse = ctype(request.GetResponse() ,FtpWebResponse)

Dim responseStream As Stream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(responseStream)
Dim write As StreamWriter = New StreamWriter("d:/jilu.dt")
'Console.WriteLine(reader.ReadToEnd())
write.Close()
reader.Close()
response.Close()

Catch ex As Exception

End Try
End Sub

这种我也用过了,可是就是不好用呀,出现的异常和上面的一样的。
it_gz_xi 2008-05-21
  • 打赏
  • 举报
回复
VS2005有这样的一个类库
System.Net.FtpWebRequest连接FTP的
yunyun837 2008-05-21
  • 打赏
  • 举报
回复
怎么没有人回答呀,哎
yunyun837 2008-05-20
  • 打赏
  • 举报
回复

Private Sub cdl1()
Try
If IO.File.Exists("d:/jilu.dt") Then
IO.File.Delete("d:/jilu.dt")
End If
My.Computer.Network.DownloadFile("ftp://vpn.li-kkk.com/jilu.dt", "d:/jilu.dt", "admin", "admin")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧