次の方法で共有


FtpWebRequest.Timeout プロパティ

定義

要求を待機するミリ秒数を取得または設定します。

public:
 virtual property int Timeout { int get(); void set(int value); };
public override int Timeout { get; set; }
member this.Timeout : int with get, set
Public Overrides Property Timeout As Integer

プロパティ値

要求がタイムアウトするまで待機するミリ秒数を含む Int32 値。既定値は Infinite です。

例外

指定された値は 0 未満であり、 Infiniteされていません。

既に進行中の要求に対して、このプロパティに新しい値が指定されました。

次のコード例では、このプロパティを設定します。

public static bool UploadUniqueFileOnServer (Uri serverUri, string fileName)
{
    // The URI described by serverUri should use the ftp:// scheme.
    // It contains the name of the directory on the server.
    // Example: ftp://contoso.com.
    //
    // The fileName parameter identifies the file containing the data to be uploaded.

    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.UploadFileWithUniqueName;
    // Set a time limit for the operation to complete.
    request.Timeout = 600000;

    // Copy the file contents to the request stream.
    const int bufferLength = 2048;
    byte[] buffer = new byte[bufferLength];
    int count = 0;
    int readBytes = 0;
    FileStream stream = File.OpenRead(fileName);
    Stream requestStream = request.GetRequestStream();
    do
    {
        readBytes = stream.Read(buffer, 0, bufferLength);
        requestStream.Write(buffer, 0, bufferLength);
        count += readBytes;
    }
    while (readBytes != 0);

    Console.WriteLine ("Writing {0} bytes to the stream.", count);
    // IMPORTANT: Close the request stream before sending the request.
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
    Console.WriteLine("Upload status: {0}, {1}",response.StatusCode, response.StatusDescription);
    Console.WriteLine ("File name: {0}", response.ResponseUri);
    response.Close();
    return true;
}

注釈

無限値を指定するには、 Timeout プロパティを Infinite (-1) に設定します。 これが既定値です。

Timeout は、 GetResponse メソッドで行われた同期要求が応答を待機し、 GetRequestStream メソッドがストリームを待機するミリ秒数です。 リソースがタイムアウト期間内に応答しない場合、要求は Status プロパティが Timeout に設定されたWebExceptionをスローします。

TimeoutGetRequestStreamBeginGetRequestStream、または GetResponse メソッドを呼び出した後にBeginGetResponseを変更すると、InvalidOperationException例外が発生します。

ドメイン ネーム システム (DNS) クエリの戻りまたはタイムアウトには最大 15 秒かかる場合があります。要求に解決が必要なホスト名が含まれており、 Timeout を 15 秒未満の値に設定すると、要求のタイムアウトを示すために WebException がスローされるまでに 15 秒以上かかることがあります。

適用対象

こちらもご覧ください