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가 아닌 경우

이미 진행 중인 요청의 이 속성에 새 값이 지정된 경우

예제

다음 코드 예제에서는이 속성을 설정합니다.

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 = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
   request->Method = WebRequestMethods::Ftp::UploadFileWithUniqueName;

   // Don't set a time limit for the operation to complete.
   request->Timeout = System::Threading::Timeout::Infinite;

   // Copy the file contents to the request stream.
   const int bufferLength = 2048;
   array<Byte>^buffer = gcnew array<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 = dynamic_cast<FtpWebResponse^>(request->GetResponse());
   Console::WriteLine( "Upload status: {0}, {1}", response->StatusCode, response->StatusDescription );
   Console::WriteLine( "File name: {0}", response->ResponseUri );
   response->Close();
   return true;
}
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;
}

설명

무한 값을 지정하려면 속성을 Infinite (-1)로 설정합니다Timeout. 기본값입니다.

Timeout 는 메서드를 사용한 동기 요청 GetResponse 이 응답을 대기하고 메서드가 GetRequestStream 스트림을 기다리는 시간(밀리초)입니다. 리소스가 제한 시간 내에 응답하지 않으면 요청은 속성이 WebException 로 설정된 TimeoutStatus throw합니다.

, , GetResponse또는 메서드를 호출한 GetRequestStream후 를 변경 Timeout 하면 예외가 발생합니다InvalidOperationException.BeginGetResponseBeginGetRequestStream

DNS(Domain Name System) 쿼리는 반환하거나 시간 초과하는 데 최대 15초가 걸릴 수 있습니다. 요청에 해결이 필요한 호스트 이름이 포함되어 있고 15초 미만의 값으로 설정한 Timeout 경우 요청 시간 초과를 나타내기 위해 이 throw되기까지 WebException 15초 이상이 걸릴 수 있습니다.

적용 대상

추가 정보