为什么这段FTP程序不支持中文?
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Runtime.Remoting.Messaging
Public Class cFTP
Private mTCPClient As New TcpClient
Private mNetStream As NetworkStream
Private mBytes() As Byte
Private intBytesRec As Int64
Private mDataStream As NetworkStream
Private mTCPData As New TcpClient
Private mServerAddr As IPAddress
Private mFTPPort As Int32 = 21
Private mConnected As Boolean = False
Private mFTPResponse As String
Public Event ServerReplied(ByVal ServerReply As String)
Public Event ServerCalled(ByVal CallMsg As String)
Public Event ErrorOccured(ByVal ErrorCode As Integer, ByVal ErrMessage As String)
Public Event Transferring(ByVal intTransferred As Integer, ByVal intTotalFile As Integer)
Public Enum EC As Integer
NoError = 0
BuildConnectionFailed = 1
ConnectionClosingFailed = 2
DirListFailed = 3
ProttectedChannelFailed = 4
DownloadFailed = 5
UploadFailed = 6
FTPCommandFailed = 7
FTPGetFileFailed = 8
FTPPutFileFailed = 9
InvalidEntry = 30
ServerImproper = 31
ServerRejectedUser = 32
ServerRejectedPass = 33
ServerDeniedDirList = 34
InvalidFileLength = 35
DownUpLoadFailure = 36
UnknownError = 9999
End Enum
'FTP Server IP
ReadOnly Property ServerAddress() As IPAddress
Get
ServerAddress = mServerAddr
End Get
End Property
'FTP Port
ReadOnly Property FTPPort() As Int32
Get
FTPPort = mFTPPort
End Get
End Property
'Connection State
ReadOnly Property Connected() As Boolean
Get
Connected = mConnected
End Get
End Property
'FTP Server return info
ReadOnly Property FTPResponse() As String
Get
FTPResponse = mFTPResponse
mFTPResponse = ""
End Get
End Property
' Class constructor
Public Sub New(ByVal ServerAddr As IPAddress, ByVal FtpPort As Int32)
BuildConnection(ServerAddr, FtpPort)
End Sub
' Class constructor overloaded
Public Sub New(ByVal ServerAddr As String, ByVal FtpPort As Int32)
Try
BuildConnection(Dns.Resolve(ServerAddr).AddressList(0), FtpPort)
Catch err As Exception
MsgBox(err.ToString())
Me.Dispose()
End Try
End Sub
Public Delegate Sub DnsCallback(ByVal ar As IAsyncResult)
' Class destructor
Protected Sub Dispose()
If Not mConnected Then
Call Close()
End If
End Sub
'Build FTP connection
Private Sub BuildConnection(ByVal ServerAddr As IPAddress, ByVal FtpPort As Int32)
Dim strTemp As String
If FtpPort <= 0 Or FtpPort > 65535 Then
RaiseEvent ErrorOccured(EC.InvalidEntry, "Port number must be between 1 and 65535!")
Exit Sub
End If
'
mServerAddr = ServerAddr
mFTPPort = FtpPort
'
Try
mTCPClient.Connect(ServerAddr, FtpPort)
mNetStream = mTCPClient.GetStream()
strTemp = GetResponse()
If strTemp.Substring(0, 4) <> "220 " Then
If strTemp.Substring(0, 3) = "220" Then
GetResponse()
Else
RaiseEvent ErrorOccured(EC.ServerImproper, "Serever replied improperly during connection!")
End If
End If
mConnected = True
Catch err As Exception
RaiseEvent ErrorOccured(EC.BuildConnectionFailed, err.ToString())
End Try
End Sub
'
Public Sub Close()
If mConnected Then
Erase mBytes
Try
mBytes = Encoding.ASCII.GetBytes("QUIT" & vbCrLf)
mNetStream.Write(mBytes, 0, mBytes.Length)
Call GetResponse()
mTCPClient.Close()
Catch err As Exception
RaiseEvent ErrorOccured(EC.ConnectionClosingFailed, err.ToString())
Finally
mConnected = False
End Try
End If
End Sub
'Get FTP server response
Private Function GetResponse() As String
Dim strTemp As String
Do
ReDim mBytes(mTCPClient.ReceiveBufferSize)
intBytesRec = mNetStream.Read(mBytes, 0, CInt(mTCPClient.ReceiveBufferSize))
strTemp = strTemp & Encoding.ASCII.GetString(mBytes, 0, intBytesRec)
Loop While mNetStream.DataAvailable
If strTemp.Length > 0 Then
RaiseEvent ServerReplied(strTemp)
End If
mFTPResponse = mFTPResponse & strTemp
GetResponse = strTemp
End Function
'Login
Public Sub IdVerify(ByVal strID As String, ByVal strPW As String)
Dim strTemp As String
If mConnected Then
'ID
If strID.Length = 0 Then
strID = "anonymous"
End If
strTemp = "USER " & strID & vbCrLf
RaiseEvent ServerCalled(strTemp)
mBytes = Encoding.ASCII.GetBytes(strTemp)
mNetStream.Write(mBytes, 0, mBytes.Length)
strTemp = GetResponse()
If strTemp.Substring(0, 4) <> "331 " Then
RaiseEvent ErrorOccured(EC.ServerRejectedUser, "Server rejected user " & strID & "!")
Exit Sub
End If
'password
strTemp = "PASS " & strPW & vbCrLf
RaiseEvent ServerCalled(strTemp)
mBytes = Encoding.ASCII.GetBytes(strTemp)
mNetStream.Write(mBytes, 0, mBytes.Length)
strTemp = GetResponse()
If strTemp.Substring(0, 4) <> "230 " Then
RaiseEvent ErrorOccured(EC.ServerRejectedPass, "Incorrect password! Server rejected password...")
Exit Sub
End If
Application.DoEvents()
If mNetStream.DataAvailable Then
Call GetResponse()
End If
End If
End Sub
'Directory list from FTP server
Public Function DirList(Optional ByVal cDirectory As String = "..") As String
Dim priSM As New MemoryStream
Dim strTemp As String
Dim intport As Int32
If mConnected Then
Try
intPort = cmdPasv2Port()
If cDirectory = ".." Then
strTemp = "LIST -aL" & vbCrLf
Else
strTemp = "LIST " & cDirectory & vbCrLf
End If
RaiseEvent ServerCalled(strTemp)
mBytes = Encoding.ASCII.GetBytes(strTemp)
mNetStream.Write(mBytes, 0, mBytes.Length)
strTemp = GetResponse()
priSM = GetInfo(intPort)
DirList = Encoding.ASCII.GetString(priSM.ToArray, 0, priSM.Length)
strTemp = GetResponse()
If strTemp.Substring(0, 4) <> "150 " Then
RaiseEvent ErrorOccured(EC.ServerDeniedDirList, "Server denied DirListCommand!")
End If
Catch err As Exception
RaiseEvent ErrorOccured(EC.DirListFailed, err.ToString())
End Try
End If
End Function