System.Net.FtpWebRequest Class

Implements a File Transfer Protocol (FTP) client.

See Also: FtpWebRequest Members

Syntax

public sealed class FtpWebRequest : WebRequest

Remarks

To obtain an instance of System.Net.FtpWebRequest, use the WebRequest.Create(string) method. You can also use the System.Net.WebClient class to upload and download information from an FTP server. Using either of these approaches, when you specify a network resource that uses the FTP scheme (for example, "ftp://contoso.com") the System.Net.FtpWebRequest class provides the ability to programmatically interact with FTP servers.

The URI may be relative or absolute. If the URI is of the form "ftp://contoso.com/%2fpath" (%2f is an escaped '/'), then the URI is absolute, and the current directory is /path. If, however, the URI is of the form "ftp://contoso.com/path", first the .NET Framework logs into the FTP server (using the user name and password set by the FtpWebRequest.Credentials property), then the current directory is set to <UserLoginDirectory>/path.

You must have a valid user name and password for the server or the server must allow anonymous logon. You can specify the credentials used to connect to the server by setting the FtpWebRequest.Credentials property or you can include them in the Uri.UserInfo portion of the URI passed to the erload:System.Net.WebRequest.Create method. If you include Uri.UserInfo information in the URI, the FtpWebRequest.Credentials property is set to a new network credential with the specified user name and password information.

Note:

Unless the FtpWebRequest.EnableSsl property is true, all data and commands, including your user name and password information, are sent to the server in clear text. Anyone monitoring network traffic can view your credentials and use them to connect to the server. If you are connecting to an FTP server that requires credentials and supports Secure Sockets Layer (SSL), you should set FtpWebRequest.EnableSsl to true.

You must have System.Net.WebPermission to access the FTP resource; otherwise, a System.Security.SecurityException exception is thrown.

Specify the FTP command to send to the server by setting the FtpWebRequest.Method property to a value defined in the System.Net.WebRequestMethods.Ftp structure. To transmit text data, change the FtpWebRequest.UseBinary property from its default value (true) to false. For details and restrictions, see FtpWebRequest.Method.

When using an System.Net.FtpWebRequest object to upload a file to a server, you must write the file content to the request stream obtained by calling the FtpWebRequest.GetRequestStream method or its asynchronous counterparts, the FtpWebRequest.BeginGetRequestStream(AsyncCallback, object) and FtpWebRequest.EndGetRequestStream(IAsyncResult) methods. You must write to the stream and close the stream before sending the request.

Requests are sent to the server by calling the FtpWebRequest.GetResponse method or its asynchronous counterparts, the FtpWebRequest.BeginGetResponse(AsyncCallback, object) and FtpWebRequest.EndGetResponse(IAsyncResult) methods. When the requested operation completes, an System.Net.FtpWebResponse object is returned. The System.Net.FtpWebResponse object provides the status of the operation and any data downloaded from the server.

You can set a time-out value for reading or writing to the server by using the FtpWebRequest.ReadWriteTimeout property. If the time-out period is exceeded, the calling method throws a System.Net.WebException with System.Net.WebExceptionStatus set to WebExceptionStatus.Timeout.

When downloading a file from an FTP server, if the command was successful, the contents of the requested file are available in the response object's stream. You can access this stream by calling the FtpWebResponse.GetResponseStream method. For more information, see System.Net.FtpWebResponse.

If the FtpWebRequest.Proxy property is set, either directly or in a configuration file, communications with the FTP server are made through the specified proxy. If the specified proxy is an HTTP proxy, only the System.Net.WebRequestMethods.Ftp.DownloadFile, System.Net.WebRequestMethods.Ftp.ListDirectory, and System.Net.WebRequestMethods.Ftp.ListDirectoryDetails commands are supported.

Only downloaded binary content is cached; that is, content received using the System.Net.WebRequestMethods.Ftp.DownloadFile command with the FtpWebRequest.UseBinary property set to true.

Multiple System.Net.FtpWebRequests reuse existing connections, if possible.

For more information about the FTP protocol, see RFC 959, "File Transfer Protocol," available at tp://www.ietf.org/rfc/rfc959.txt.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0