FtpWebRequest.Timeout Proprietà

Definizione

Ottiene o imposta il numero di millisecondi di attesa per una richiesta.

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

Valore della proprietà

Valore Int32 contenente il numero di millisecondi di attesa prima del timeout di una richiesta. Il valore predefinito è Infinite.

Eccezioni

Il valore specificato è minore di zero e non è Infinite.

È stato specificato un nuovo valore per questa proprietà per una richiesta già in corso.

Esempio

Nell'esempio di codice seguente questa proprietà viene impostata.

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

Commenti

Per specificare un valore infinito, impostare la Timeout proprietà su Infinite (-1). Rappresenta il valore predefinito.

Timeout è il numero di millisecondi che una richiesta sincrona effettuata con il GetResponse metodo attende una risposta e che il GetRequestStream metodo attende un flusso. Se una risorsa non risponde entro il periodo di timeout, la richiesta genera un WebException oggetto con la Status proprietà impostata su Timeout.

La modifica Timeout dopo la chiamata al GetRequestStreammetodo , BeginGetRequestStreamGetResponse, o BeginGetResponse causa un'eccezioneInvalidOperationException.

La restituzione o il timeout di una query DNS (Domain Name System) può richiedere fino a 15 secondi. Se la richiesta contiene un nome host che richiede la risoluzione e si imposta Timeout su un valore inferiore a 15 secondi, potrebbero essere necessari 15 secondi o più prima che venga generata un'eccezione WebException per indicare un timeout nella richiesta.

Si applica a

Vedi anche