FtpWebResponse Class

Namespace: System.Net

Assembly: System (in System.dll)

Syntax


public class FtpWebResponse : WebResponse
            

Inheritance

Object
MarshalByRefObject
WebRequest
WebResponse
FtpWebResponse

Remarks

The FtpWebResponse class represents the response from a FtpWebRequest.

It provides access to the data returned by the FTP server, including headers and the response stream. This class implements the IDisposable interface, so it should be disposed of when no longer needed to release resources.

Properties

Name Description
ContentLength Gets the content length of the FTP server response.
ContentType Gets the content type of the FTP server response.
ExitCode Gets the exit code returned by the FTP server.
FtpErrorResponse Gets the detailed error message returned by the FTP server.
LastModified Gets the last modified date and time of the requested resource.
Method Gets the FTP command that was used to obtain the response.
ResponseUri Gets the Uri that identifies the FTP server resource.
StatusCode Gets the status code returned by the FTP server.
StatusDescription Gets a description of the FTP status code.

Methods

Name Description
Close() Closes the FTP response stream and releases resources.
Dispose() Releases all resources used by the FtpWebResponse object.
GetResponseStream() Returns the data stream of the FTP server response.

Example

The following example demonstrates how to download a file from an FTP server using FtpWebRequest and read the response using FtpWebResponse.


using System;
using System.IO;
using System.Net;
using System.Text;

public class FtpDownloadExample
{
    public static void DownloadFile(string ftpUrl, string localFileName)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential("anonymous", "user@example.com"); // Replace with your credentials

        try
        {
            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Console.WriteLine($"Download Complete, Status: {response.StatusDescription}");

                using (Stream responseStream = response.GetResponseStream())
                using (FileStream fileStream = File.Create(localFileName))
                {
                    responseStream.CopyTo(fileStream);
                }
            }
            Console.WriteLine($"File '{localFileName}' downloaded successfully.");
        }
        catch (WebException ex)
        {
            Console.Error.WriteLine($"FTP Error: {ex.Message}");
            if (ex.Response is FtpWebResponse errorResponse)
            {
                Console.Error.WriteLine($"FTP Status Code: {errorResponse.StatusCode}");
                Console.Error.WriteLine($"FTP Error Response: {errorResponse.FtpErrorResponse}");
            }
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"An error occurred: {ex.Message}");
        }
    }

    public static void Main(string[] args)
    {
        // Example usage:
        // string ftpUrl = "ftp://ftp.example.com/path/to/your/file.txt";
        // string localFileName = "downloaded_file.txt";
        // DownloadFile(ftpUrl, localFileName);

        Console.WriteLine("Example usage commented out. Please uncomment and configure with your FTP details.");
    }
}
            

See Also