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为 。

例外

指定的值是小于零,且不是 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;
}

注解

若要指定无限值,请将 Timeout 属性设置为 Infinite (-1) 。 这是默认值。

Timeout 是使用 GetResponse 方法发出的同步请求等待响应和 GetRequestStream 方法等待流的毫秒数。 如果资源在超时期限内未响应,则请求将引发 , WebException 并将 Status 属性设置为 Timeout

调用 、、 或 方法后更改Timeout会导致InvalidOperationException异常。BeginGetResponseGetResponseBeginGetRequestStreamGetRequestStream

域名系统 (DNS) 查询可能需要长达 15 秒才能返回或超时。如果请求包含需要解析的主机名,并且你设置为 Timeout 小于 15 秒的值,则可能需要 15 秒或更多时间才能 WebException 引发 来指示请求超时。

适用于

另请参阅