แก้ไข

แชร์ผ่าน


FtpWebRequest.UsePassive Property

Definition

Gets or sets the behavior of a client application's data transfer process.

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

Property Value

false if the client application's data transfer process listens for a connection on the data port; otherwise, true if the client should initiate a connection on the data port. The default value is true.

Exceptions

A new value was specified for this property for a request that is already in progress.

Examples

The following code example retrieves and displays property values for a specified FtpWebRequest object.

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);
}

Remarks

Setting the UsePassive property to true sends the "PASV" command to the server. This command requests the server to listen on a data port and to wait for a connection rather than initiate one upon receipt of a transfer command.

For a description of the behaviors that are specified using UsePassive, see RFC 959: "File Transfer Protocol", Section 3.2: "Establishing Data Connections" and Section 4.1.2: "Transfer Parameter Commands".

Changing UsePassive after calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method causes an InvalidOperationException exception.

If UsePassive is set to true, the FTP server may not send the size of the file, and download progress can always be zero. If UsePassive is set to false, a firewall can raise an alert and block the file download.

Applies to

See also