HttpClient.Timeout 屬性

定義

取得或設定要求逾時前等候的時間長度。Gets or sets the timespan to wait before the request times out.

public:
 property TimeSpan Timeout { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan Timeout { get; set; }
member this.Timeout : TimeSpan with get, set
Public Property Timeout As TimeSpan

屬性值

TimeSpan

要求逾時前等候的時間長度。The timespan to wait before the request times out.

例外狀況

指定的逾時值小於或等於零,而且不是 InfiniteTimeSpanThe timeout specified is less than or equal to zero and is not InfiniteTimeSpan.

作業已經在目前的執行個體上啟動。An operation has already been started on the current instance.

目前的執行個體已經過處置。The current instance has been disposed.

範例

下列範例會設定 Timeout 屬性。The following example sets the Timeout property.

HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(10);

備註

預設值為 100,000 毫秒 (100 秒)。The default value is 100,000 milliseconds (100 seconds).

若要設定無限的超時時間,請將屬性值設定為 InfiniteTimeSpanTo set an infinite timeout, set the property value to InfiniteTimeSpan.

網域名稱系統 (DNS) 查詢最多可能需要15秒的時間才會傳回或超時。如果您的要求包含需要解析的主機名稱,且您將設定 Timeout 為小於15秒的值,則可能需要15秒以上的時間才會擲回, WebException 以表示要求的超時時間。A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

使用這個實例的所有要求都會套用相同的超時時間 HttpClientThe same timeout will apply for all requests using this HttpClient instance. 您也可以使用工作上的,為個別要求設定不同的超時 CancellationTokenSourceYou may also set different timeouts for individual requests using a CancellationTokenSource on a task. 請注意,只會套用兩個超時的較短。Note that only the shorter of the two timeouts will apply.

適用於