FtpWebRequest.Timeout Właściwość

Definicja

Pobiera lub ustawia liczbę milisekund oczekiwania na żądanie.

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

Wartość właściwości

Wartość zawierająca Int32 liczbę milisekund oczekiwania przed upływem limitu czasu żądania. Wartość domyślna to Infinite.

Wyjątki

Określona wartość jest mniejsza niż zero i nie Infinitejest .

Dla tej właściwości określono nową wartość dla żądania, które jest już w toku.

Przykłady

Poniższy przykład kodu ustawia tę właściwość.

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

Uwagi

Aby określić nieskończoną wartość, ustaw Timeout właściwość na Infinite (-1). Jest to wartość domyślna.

Timeout to liczba milisekund, które synchroniczne żądanie wykonane za GetResponse pomocą metody czeka na odpowiedź i że GetRequestStream metoda czeka na strumień. Jeśli zasób nie odpowiada w przedziale czasu, żądanie zgłasza WebException wyjątek z właściwością ustawioną Status na Timeoutwartość .

Zmiana Timeout po wywołaniu GetRequestStreammetody , BeginGetRequestStream, GetResponselub BeginGetResponse powoduje InvalidOperationException wyjątek.

Zapytanie systemu nazw domen (DNS) może potrwać do 15 sekund, aby zwrócić lub upłynął limit czasu. Jeśli żądanie zawiera nazwę hosta, która wymaga rozwiązania i ustawiono Timeout wartość mniejszą niż 15 sekund, może upłynąć 15 sekund lub więcej, zanim WebException zostanie zgłoszony wyjątek , aby wskazać limit czasu żądania.

Dotyczy

Zobacz też