Implements a File Transfer Protocol (FTP) client.
| C# | Visual Basic |
public class FtpWebRequest : WebRequest
Public Class FtpWebRequest _ Inherits WebRequest
| All Members | Methods | Properties | |||
| Icon | Member | Description |
|---|---|---|
| Abort()()()() |
Aborts the Request
(Inherited from WebRequest.) | |
| AuthenticationLevel |
Gets or sets values indicating the level of authentication and impersonation used for this request.
(Inherited from WebRequest.) | |
| BeginGetRequestStream(AsyncCallback, Object) | HostProtectionAttribute.
When overridden in a descendant class, provides an asynchronous version of the GetRequestStream()()()() method.
(Inherited from WebRequest.) | |
| BeginGetResponse(AsyncCallback, Object) |
Begins sending a request and receiving a response from an FTP server asynchronously.
(Overrides WebRequest.BeginGetResponse(AsyncCallback, Object).) | |
| CachePolicy |
Gets or sets the cache policy for this request.
(Inherited from WebRequest.) | |
| ConnectionGroupName |
When overridden in a descendant class, gets or sets the name of the connection group for the request.
(Inherited from WebRequest.) | |
| ContentLength |
This property supports the .NET Compact Framework infrastructure and is not intended to be used directly from your code.
Gets or sets a value that is ignored by the FtpWebRequest class.
(Overrides WebRequest.ContentLength.) | |
| ContentType |
This property supports the .NET Compact Framework infrastructure and is not intended to be used directly from your code.
Always throws a NotSupportedException.
(Overrides WebRequest.ContentType.) | |
| CreateObjRef(Type) |
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject.) | |
| Credentials |
Gets or sets the credentials used to communicate with the FTP server.
(Overrides WebRequest.Credentials.) | |
| EndGetRequestStream(IAsyncResult) |
When overridden in a descendant class, returns a Stream for writing data to the Internet resource.
(Inherited from WebRequest.) | |
| EndGetResponse(IAsyncResult) |
Ends a pending asynchronous operation started with BeginGetResponse(AsyncCallback, Object).
(Overrides WebRequest.EndGetResponse(IAsyncResult).) | |
| Equals(Object) | (Inherited from Object.) | |
| Finalize()()()() |
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
(Inherited from Object.) | |
| GetHashCode()()()() |
Serves as a hash function for a particular type.
(Inherited from Object.) | |
| GetLifetimeService()()()() |
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject.) | |
| GetObjectData(SerializationInfo, StreamingContext) |
Populates a SerializationInfo with the data needed to serialize the target object.
(Inherited from WebRequest.) | |
| GetRequestStream()()()() |
Retrieves the stream used to upload data to an FTP server.
(Overrides WebRequest.GetRequestStream()()()().) | |
| GetResponse()()()() |
Returns the FTP server response.
(Overrides WebRequest.GetResponse()()()().) | |
| GetType()()()() |
Gets the Type of the current instance.
(Inherited from Object.) | |
| Headers |
This property supports the .NET Compact Framework infrastructure and is not intended to be used directly from your code.
Gets an empty WebHeaderCollection object.
(Overrides WebRequest.Headers.) | |
| ImpersonationLevel |
Gets or sets the impersonation level for the current request.
(Inherited from WebRequest.) | |
| InitializeLifetimeService()()()() |
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject.) | |
| MemberwiseClone(Boolean) |
Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject.) | |
| MemberwiseClone()()()() |
Creates a shallow copy of the current Object.
(Inherited from Object.) | |
| Method |
Gets or sets the command to send to the FTP server.
(Overrides WebRequest.Method.) | |
| PreAuthenticate |
This API supports the .NET Compact Framework infrastructure and is not intended to be used directly from your code.
(Overrides WebRequest.PreAuthenticate.) | |
| Proxy | (Overrides WebRequest.Proxy.) | |
| RegisterPrefix()()()() |
Register this class with the WebRequest class.
| |
| RenameTo |
Gets or sets the new name of a file being renamed.
| |
| RequestUri |
Gets the URI requested by this instance.
(Overrides WebRequest.RequestUri.) | |
| Timeout |
Gets or sets the number of milliseconds to wait for a request.
(Overrides WebRequest.Timeout.) | |
| ToString()()()() | (Inherited from Object.) | |
| UseBinary |
Gets or sets a Boolean value that specifies the data type for file transfers.
| |
| UseDefaultCredentials |
When overridden in a descendant class, gets or sets a Boolean value that controls whether DefaultCredentials are sent with requests.
(Inherited from WebRequest.) | |
| UsePassive |
Gets or sets the behavior of a client application's data transfer process.
|
Equivalent to System.Net.FtpWebRequest
You must call the static method RegisterPrefix()()()() before this class can be used with Create(String). You only need to call RegisterPrefix()()()() once in your application.
To obtain an instance of FtpWebRequest, use the Create(String) method after calling RegisterPrefix()()()(). You can also use the 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 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 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 Credentials property or you can include them in the UserInfo portion of the URI passed to the Create(String) method. If you include UserInfo information in the URI, the Credentials property is set to a new network credential with the specified user name and password information.The following code example demonstrates deleting a file from an FTP server.
CopyC#
public static bool DeleteFileOnServer(Uri serverUri) { FtpWebRequest.RegisterPrefix(); // The serverUri parameter should use the ftp:// scheme. // It contains the name of the server file that is to be deleted. // Example: ftp://contoso.com/someFile.txt. // 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.DeleteFile; FtpWebResponse response = (FtpWebResponse) request.GetResponse(); Console.WriteLine("Delete status: {0}",response.StatusDescription); response.Close(); return true; }
The following code example demonstrates downloading a file from an FTP server by using the WebClient class.
CopyC#
public static bool DisplayFileFromServer(Uri serverUri) { // The serverUri parameter should start with the ftp:// scheme. if (serverUri.Scheme != Uri.UriSchemeFtp) { return false; } // Get the object used to communicate with the server. WebClient request = new WebClient(); // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); try { byte [] newFileData = request.DownloadData(serverUri.ToString()); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); Console.WriteLine(fileString); } catch (WebException e) { Console.WriteLine(e.ToString()); } return true; }
The following code example demonstrates using asynchronous operations to upload a file to an FTP server.
CopyC#
using System; using System.Net; using InTheHand.Net; using System.Threading; using System.IO; using InTheHand.IO; namespace Examples.InTheHand.Net { public class FtpState { private ManualResetEvent wait; private FtpWebRequest request; private string fileName; private Exception operationException = null; string status; public FtpState() { wait = new ManualResetEvent(false); } public ManualResetEvent OperationComplete { get {return wait;} } public FtpWebRequest Request { get {return request;} set {request = value;} } public string FileName { get {return fileName;} set {fileName = value;} } public Exception OperationException { get {return operationException;} set {operationException = value;} } public string StatusDescription { get {return status;} set {status = value;} } } public class AsynchronousFtpUpLoader { // Command line arguments are two strings: // 1. The url that is the name of the file being uploaded to the server. // 2. The name of the file on the local machine. // public static void Main(string[] args) { FtpWebRequest.RegisterPrefix(); // Create a Uri instance with the specified URI string. // If the URI is not correctly formed, the Uri constructor // will throw an exception. ManualResetEvent waitObject; Uri target = new Uri (args[0]); string fileName = args[1]; FtpState state = new FtpState(); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target); request.Method = WebRequestMethods.Ftp.UploadFile; // This example uses anonymous logon. // The request is anonymous by default; the credential does not have to be specified. // The example specifies the credential only to // control how actions are logged on the server. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); // Store the request in the object that we pass into the // asynchronous operations. state.Request = request; state.FileName = fileName; // Get the event to wait on. waitObject = state.OperationComplete; // Asynchronously get the stream for the file contents. request.BeginGetRequestStream( new AsyncCallback (EndGetStreamCallback), state ); // Block the current thread until all operations are complete. waitObject.WaitOne(); // The operations either completed or threw an exception. if (state.OperationException != null) { throw state.OperationException; } else { Console.WriteLine("The operation completed - {0}", state.StatusDescription); } } private static void EndGetStreamCallback(IAsyncResult ar) { FtpState state = (FtpState) ar.AsyncState; Stream requestStream = null; // End the asynchronous call to get the request stream. try { requestStream = state.Request.EndGetRequestStream(ar); // Copy the file contents to the request stream. FileStream stream = File.OpenRead(state.FileName); stream.CopyTo(requestStream); stream.Close(); Console.WriteLine ("Writing {0} bytes to the stream.", stream.Length); // IMPORTANT: Close the request stream before sending the request. requestStream.Close(); // Asynchronously get the response to the upload request. state.Request.BeginGetResponse( new AsyncCallback (EndGetResponseCallback), state ); } // Return exceptions to the main application thread. catch (Exception e) { Console.WriteLine("Could not get the request stream."); state.OperationException = e; state.OperationComplete.Set(); return; } } // The EndGetResponseCallback method // completes a call to BeginGetResponse. private static void EndGetResponseCallback(IAsyncResult ar) { FtpState state = (FtpState) ar.AsyncState; FtpWebResponse response = null; try { response = (FtpWebResponse) state.Request.EndGetResponse(ar); response.Close(); state.StatusDescription = response.StatusDescription; // Signal the main application thread that // the operation is complete. state.OperationComplete.Set(); } // Return exceptions to the main application thread. catch (Exception e) { Console.WriteLine ("Error getting response."); state.OperationException = e; state.OperationComplete.Set(); } } } }
| Object | |||
| MarshalByRefObject | |||
| WebRequest | |||
| FtpWebRequest | |||