英語で読む

次の方法で共有


FtpWebResponse.GetResponseStream メソッド

定義

FTP サーバーから送信された応答データを格納しているストリームを取得します。

public override System.IO.Stream GetResponseStream();

戻り値

応答で返されたデータを格納している読み取り可能な Stream インスタンス。サーバーによって返された応答データがない場合は Null

例外

応答でデータ ストリームが返されませんでした。

次のコード例は、要求の応答ストリームを取得する方法を ListDirectory 示しています。

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

注釈

データを読み取った後、ストリームを閉じる必要があります。 ストリームを含むオブジェクトを閉じると、 FtpWebResponse ストリームは自動的に閉じられます。

要求メソッドが または ListDirectoryでない限り、例外がDownloadFileスローされます。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください