FtpWebRequest.UsePassive 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置客户端应用程序的数据传输过程的行为。
public:
property bool UsePassive { bool get(); void set(bool value); };
public bool UsePassive { get; set; }
member this.UsePassive : bool with get, set
Public Property UsePassive As Boolean
属性值
如果客户端应用程序的数据传输过程侦听数据端口上的连接,则为 false
;如果客户端应在数据端口上启动连接,则为 true
。 默认值是 true
。
例外
对于一个已在进行的请求为此属性指定了一个新值。
示例
下面的代码示例检索并显示指定 FtpWebRequest 对象的属性值。
private:
// DisplayRequestProperties prints a request's properties.
// This method should be called after the request is sent to the server.
static void DisplayRequestProperties( FtpWebRequest^ request )
{
Console::WriteLine( "User {0} {1}", request->Credentials->GetCredential( request->RequestUri, "basic" )->UserName, request->RequestUri );
Console::WriteLine( "Request: {0} {1}", request->Method, request->RequestUri );
Console::WriteLine( "Passive: {0} Keep alive: {1} Binary: {2} Timeout: {3}.", request->UsePassive, request->KeepAlive, request->UseBinary, request->Timeout == -1 ? "none" : request->Timeout.ToString() );
IWebProxy^ proxy = request->Proxy;
if ( proxy )
{
Console::WriteLine( "Proxy: {0}", proxy->GetProxy( request->RequestUri ) );
}
else
{
Console::WriteLine( "Proxy: (none)" );
}
Console::WriteLine( "ConnectionGroup: {0}", request->ConnectionGroupName == nullptr ? "none" : request->ConnectionGroupName );
Console::WriteLine( "Encrypted connection: {0}", request->EnableSsl );
Console::WriteLine("Method: {0}", request->Method);
}
// DisplayRequestProperties prints a request's properties.
// This method should be called after the request is sent to the server.
private static void DisplayRequestProperties(FtpWebRequest request)
{
Console.WriteLine("User {0} {1}",
request.Credentials.GetCredential(request.RequestUri,"basic").UserName,
request.RequestUri
);
Console.WriteLine("Request: {0} {1}",
request.Method,
request.RequestUri
);
Console.WriteLine("Passive: {0} Keep alive: {1} Binary: {2} Timeout: {3}.",
request.UsePassive,
request.KeepAlive,
request.UseBinary,
request.Timeout == -1 ? "none" : request.Timeout.ToString()
);
IWebProxy proxy = request.Proxy;
if (proxy != null)
{
Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
}
else
{
Console.WriteLine("Proxy: (none)");
}
Console.WriteLine("ConnectionGroup: {0}",
request.ConnectionGroupName == null ? "none" : request.ConnectionGroupName
);
Console.WriteLine("Encrypted connection: {0}",
request.EnableSsl);
Console.WriteLine("Method: {0}", request.Method);
}
注解
将 UsePassive 属性设置为 true
会将 “PASV"
命令发送到服务器。 此命令请求服务器侦听数据端口并等待连接,而不是在收到传输命令时启动连接。
有关使用 UsePassive指定的行为的说明,请参阅 RFC 959:“文件传输协议”、第 3.2 节:“建立数据连接”和第 4.1.2 节:“传输参数命令”。
调用 GetRequestStream、、 GetResponseBeginGetRequestStream或 BeginGetResponse 方法后更改UsePassive会导致InvalidOperationException异常。
如果 UsePassive 设置为 true
,则 FTP 服务器可能不会发送文件大小,并且下载进度始终可以为零。 如果 UsePassive 设置为 false
,防火墙可能会引发警报并阻止文件下载。