注意:這個屬性是 .NET Framework 2.0 版的新功能。
取得或設定等待要求的毫秒數。
命名空間: System.Net
組件: System (在 system.dll 中)
Public Overrides Property Timeout As Integer
Dim instance As FtpWebRequest
Dim value As Integer
value = instance.Timeout
instance.Timeout = value
public override int Timeout { get; set; }
public:
virtual property int Timeout {
int get () override;
void set (int value) override;
}
/** @property */
public int get_Timeout ()
/** @property */
public void set_Timeout (int value)
public override function get Timeout () : int
public override function set Timeout (value : int)
屬性值
Int32 值,包含要求逾時之前等待的毫秒數。預設值為 Infinite。
若要指定無限值,請將 Timeout 屬性設為 Infinite (-1)。這是預設值。
Timeout 為使用 GetResponse 方法執行的同步要求等待回應,以及使用 GetRequestStream 方法執行的同步要求等待資料流的毫秒數。如果資源未在逾時期限內回應,要求會擲回 WebException,並將 Status 屬性設為 Timeout。
呼叫 GetRequestStream、BeginGetRequestStream、GetResponse 或 BeginGetResponse 方法之後會變更 Timeout,而造成 InvalidOperationException 例外狀況。
網域名稱系統 (Domain Name System,DNS) 查詢可能至多需要 15 秒才會傳回或逾時。如果要求包含需要解析的主機名稱,並且 Timeout 設為小於 15 秒的值,則可能需要 15 秒以上才會擲回 WebException,指出要求逾時。
下列程式碼範例會設定這個屬性。
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;
}
Windows 98、 Windows 2000 SP4、 Windows Millennium Edition、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
.NET Framework
支援版本:2.0