HttpClient.Timeout HttpClient.Timeout HttpClient.Timeout HttpClient.Timeout Property

定义

获取或设置请求超时前等待的时间跨度。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

属性值

请求超时前等待的时间跨度。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.

注解

默认值是 100,000 毫秒(100 秒)。The default value is 100,000 milliseconds (100 seconds).

若要设置无限超时,请将属性值设置InfiniteTimeSpan为。To 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.

使用此HttpClient实例的所有请求都将使用相同的超时值。The same timeout will apply for all requests using this HttpClient instance. 你还可以使用任务上的CancellationTokenSource为单个请求设置不同的超时。You 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.

示例Example

下面的示例设置Timeout属性。The following example sets the Timeout property.

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

适用于