FtpWebRequest.EnableSsl 属性

定义

获取或设置 Boolean,它指定是否使用 SSL 连接。

public:
 property bool EnableSsl { bool get(); void set(bool value); };
public bool EnableSsl { get; set; }
member this.EnableSsl : bool with get, set
Public Property EnableSsl As Boolean

属性值

如果控制和数据传输是加密的,则为 true;否则为 false。 默认值是 false

例外

到 FTP 服务器的连接已经建立。

示例

下面的代码示例使用加密连接从 FTP 服务器下载目录列表。

public static bool ListFilesOnServerSsl(Uri serverUri)
{
    // The serverUri should start with the ftp:// scheme.
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.EnableSsl = true;

    // Get the ServicePoint object used for this request, and limit it to one connection.
    // In a real-world application you might use the default number of connections (2),
    // or select a value that works best for your application.

    ServicePoint sp = request.ServicePoint;
    Console.WriteLine("ServicePoint connections = {0}.", sp.ConnectionLimit);
    sp.ConnectionLimit = 1;

    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
     Console.WriteLine("The content length is {0}", response.ContentLength);
    // The following streams are used to read the data returned from the server.
    Stream responseStream = null;
    StreamReader readStream = null;
    try
    {
        responseStream = response.GetResponseStream();
        readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);

        if (readStream != null)
        {
            // Display the data received from the server.
            Console.WriteLine(readStream.ReadToEnd());
        }
        Console.WriteLine("List status: {0}",response.StatusDescription);
    }
    finally
    {
        if (readStream != null)
        {
            readStream.Close();
        }
        if (response != null)
        {
            response.Close();
        }
    }

    Console.WriteLine("Banner message: {0}",
        response.BannerMessage);

    Console.WriteLine("Welcome message: {0}",
        response.WelcomeMessage);

    Console.WriteLine("Exit message: {0}",
        response.ExitMessage);
    return true;
}

注解

注意

EnableSsl除非 属性为 true,否则所有数据和命令(包括用户名和密码信息)都以明文形式发送到服务器。 监视网络流量的任何人都可以查看你的凭据并使用它们连接到服务器。 如果要连接到需要凭据并支持 SSL 的 FTP 服务器,则应将 设置为 EnableSsltrue

命令 "AUTH TLS" 将发送到服务器以请求加密的会话。 如果服务器无法识别此命令,则会收到异常 WebException

适用于

另请参阅