FtpWebResponse.GetResponseStream Method

定義

FTP サーバーから送信された応答データを格納しているストリームを取得します。Retrieves the stream that contains response data sent from an FTP server.

C#
public override System.IO.Stream GetResponseStream ();

戻り値

応答で返されたデータを格納している読み取り可能な Stream インスタンス。サーバーによって返された応答データがない場合は NullA readable Stream instance that contains data returned with the response; otherwise, Null if no response data was returned by the server.

例外

応答でデータ ストリームが返されませんでした。The response did not return a data stream.

次のコード例では、応答ストリームの取得を示しています、ListDirectory要求。The following code example demonstrates getting the response stream for a ListDirectory request.

C#
public static bool ListFilesOnServer(Uri serverUri)
{
    // The serverUri should start with the ftp:// scheme.
    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.ListDirectory;
    
    // Get the ServicePoint object used for this request, and limit it to one connection.
    // In a real-world application you might use the default number of connections (2),
    // or select a value that works best for your application.
    
    ServicePoint sp = request.ServicePoint;
    Console.WriteLine("ServicePoint connections = {0}.", sp.ConnectionLimit);
    sp.ConnectionLimit = 1;
    
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
     
    // The following streams are used to read the data returned from the server.
    Stream responseStream = null;
    StreamReader readStream = null;
    try
    {
        responseStream = response.GetResponseStream(); 
        readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);

        if (readStream != null)
        {
            // Display the data received from the server.
            Console.WriteLine(readStream.ReadToEnd());
        } 
        Console.WriteLine("List status: {0}",response.StatusDescription);            
    }
    finally
    {
        if (readStream != null)
        {
            readStream.Close();
        }
        if (response != null)
        {
            response.Close();
        }
    }
   
    return true;
}

注釈

データの読み込み後にストリームを閉じる必要があります。After reading the data, you must close the stream. 閉じるときに自動的に、ストリームが閉じている、FtpWebResponseそれを含んでいるオブジェクト。The stream is automatically closed when you close the FtpWebResponse object that contains it.

要求メソッドがない限り、例外がスローされたDownloadFileまたはListDirectoryします。An exception is thrown unless the request method is DownloadFile or ListDirectory.

適用対象

.NET Core

3.0 Preview 2 2.2 2.1 2.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0

.NET Standard

2.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0

こちらもご覧ください