コード例 #1
0
        public static Encoding GetEncoding(ETextEncoding encoding)
        {
            switch (encoding)
            {
            case ETextEncoding.ASCII:
                return(Encoding.ASCII);

            case ETextEncoding.BigEndianUnicode:
                return(Encoding.BigEndianUnicode);

            case ETextEncoding.ANSI:
                return(Encoding.Default);

            case ETextEncoding.Unicode:
                return(Encoding.Unicode);

            case ETextEncoding.UTF32:
                return(Encoding.UTF32);

            case ETextEncoding.UTF7:
                return(Encoding.UTF7);

            case ETextEncoding.UTF8:
                return(Encoding.UTF8);

            default:
                return((Encoding)null);
            }
        }
コード例 #2
0
ファイル: FTPSClient.cs プロジェクト: seeseekey/CSCL
        /// <summary>
        /// Connects to a FTP server using the provided parameters. 
        /// The default representation tipe is set to Binary.
        /// The text encoding is set to UTF8, if supported by the server via the FEAT command.
        /// </summary>
        /// <param name="hostname"></param>
        /// <param name="port"></param>
        /// <param name="credential"></param>
        /// <param name="sslSupportMode"></param>
        /// <param name="userValidateServerCertificate"></param>
        /// <param name="x509ClientCert"></param>
        /// <param name="sslMinKeyExchangeAlgStrength"></param>
        /// <param name="sslMinCipherAlgStrength"></param>
        /// <param name="sslMinHashAlgStrength"></param>
        /// <param name="timeout">Connection timeout in ms. <c>null</c> can be specifiad to keep the default value of 120s.</param>
        /// <param name="useCtrlEndPointAddressForData"><c>true</c> to use the control channel remote address for data connections instead of the address returned by PASV</param>
        /// <returns>The text of the \"welcome message\" sent by the server.</returns>
        public string Connect(string hostname, int port, NetworkCredential credential, ESSLSupportMode sslSupportMode, 
                            RemoteCertificateValidationCallback userValidateServerCertificate, X509Certificate x509ClientCert, 
                            int sslMinKeyExchangeAlgStrength, int sslMinCipherAlgStrength, int sslMinHashAlgStrength, 
                            int? timeout, bool useCtrlEndPointAddressForData)
        {
            Close();

            // Anonymous authentication
            if (credential == null)
                credential = new NetworkCredential(anonUsername, anonPassword);

            if (timeout != null)
                this.timeout = timeout.Value;

            this.sslClientCert = x509ClientCert;

            this.userValidateServerCertificate = userValidateServerCertificate;

            this.sslMinKeyExchangeAlgStrength = sslMinKeyExchangeAlgStrength;
            this.sslMinCipherAlgStrength = sslMinCipherAlgStrength;
            this.sslMinHashAlgStrength = sslMinHashAlgStrength;

            this.sslSupportRequestedMode = sslSupportMode;
            this.sslSupportCurrentMode = sslSupportMode;

            this.useCtrlEndPointAddressForData = useCtrlEndPointAddressForData;

            sslInfo = null;

            features = null;

            transferMode = ETransferMode.ASCII;
            textEncoding = ETextEncoding.ASCII;

            bannerMessage = null;
            welcomeMessage = null;            

            currDirStack.Clear();

            // Ok, member initialization is done. Start with setting up a control connection
            SetupCtrlConnection(hostname, port, Encoding.ASCII);

            // Used later for SSL/TLS auth
            this.hostname = hostname;

            // Implicit SSL/TLS
            bool isImplicitSsl = (sslSupportMode & ESSLSupportMode.Implicit) == ESSLSupportMode.Implicit;
            if (isImplicitSsl)
                SwitchCtrlToSSLMode();

            // Wait fot server message
            bannerMessage = GetReply().Message;

            // Explicit SSL/TLS
            if (!isImplicitSsl)
                SslControlChannelCheckExplicitEncryptionRequest(sslSupportMode);

            // Login. Note that a password might not be required
            // TODO: check if the welcomeMessage is returned by the USER command in case the PASS command is not required.  
            if(UserCmd(credential.UserName))
                welcomeMessage = PassCmd(credential.Password);

            GetFeaturesFromServer();

            if (IsControlChannelEncrypted && !isImplicitSsl)
            {
                SslDataChannelCheckExplicitEncryptionRequest();

                if ((sslSupportMode & ESSLSupportMode.ControlChannelRequested) != ESSLSupportMode.ControlChannelRequested)
                    SSlCtrlChannelCheckRevertToClearText();
            }

            try
            {
                // This is required by some FTP servers and must precede any OPTS command
                if (CheckFeature("CLNT"))
                    ClntCmd(clntName);

                // Set UTF8 as character encoding, but only if listed among the FEAT features
                if (CheckFeature("UTF8"))
                    SetTextEncoding(ETextEncoding.UTF8);
            }
            catch (Exception ex)
            {
                //TODO: add warning info
            }

            // Default binary transfers
            SetTransferMode(ETransferMode.Binary);

            return welcomeMessage;
        }
コード例 #3
0
ファイル: FTPSClient.cs プロジェクト: seeseekey/CSCL
 /// <summary>
 /// Set the given text encoding.
 /// </summary>
 /// <param name="textEncoding"></param>
 public void SetTextEncoding(ETextEncoding textEncoding)
 {
     OptsCmd("UTF8 " + (textEncoding == ETextEncoding.UTF8 ? "ON" : "OFF"));
     this.textEncoding = textEncoding;
 }