HttpCompletionOption 열거형
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
응답을 사용할 수 있는 즉시 또는 콘텐츠를 포함한 전체 응답 메시지를 읽은 후 HttpClient 작업이 완료된 것으로 간주되어야 하는지 여부를 나타냅니다.
public enum class HttpCompletionOption
public enum HttpCompletionOption
type HttpCompletionOption =
Public Enum HttpCompletionOption
- 상속
필드
| Name | 값 | Description |
|---|---|---|
| ResponseContentRead | 0 | 콘텐츠를 포함한 전체 응답을 읽은 후 작업이 완료되어야 합니다. |
| ResponseHeadersRead | 1 | 응답을 사용할 수 있고 헤더를 읽는 즉시 작업이 완료되어야 합니다. 콘텐츠가 아직 읽혀지지 않았습니다. |
설명
경고
이 값은 HttpCompletionOption 응답을 읽을 때 옵션에 지정된 시간 제한의 HttpClient 범위에 영향을 줍니다. 해당 메서드의 HttpClient 시간 제한은 해당 메서드가 완료/반환될 때까지 항상 관련 호출된 메서드에 적용됩니다. 결정적으로 이 옵션을 사용하는 ResponseHeadersRead 경우 시간 제한은 헤더가 종료되고 콘텐츠가 시작되는 위치까지만 적용됩니다. 서버에서 상태 줄과 헤더를 즉시 반환하지만 콘텐츠를 반환하는 데 시간이 너무 오래 걸리는 경우 콘텐츠 읽기 작업을 별도로 시간 초과해야 합니다. 이 고려 사항은 속성에도 MaxResponseContentBufferSize 적용됩니다. 이 제한은 .를 사용하는 ResponseContentRead경우에만 적용됩니다. 다음은 이 점을 보여 주는 예제입니다.
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(30);
httpClient.MaxResponseContentBufferSize = 1_000; // This will be ignored
// Because we're specifying the ResponseHeadersRead option,
// the 30-second timeout applies only up until the headers are received.
// It does not affect future operations that interact with the response content.
using HttpResponseMessage response = await httpClient.GetAsync(
"http://localhost:12345/",
HttpCompletionOption.ResponseHeadersRead);
// Do other checks that don't rely on the content first, like status code validation.
response.EnsureSuccessStatusCode();
// Since the HttpClient.Timeout will not apply to reading the content,
// you must enforce it yourself, for example by using a CancellationTokenSource.
using var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(15));
// If you wish to enforce the MaxResponseContentBufferSize limit as well,
// you can do so by calling the LoadIntoBufferAsync helper first.
await response.Content.LoadIntoBufferAsync(1_000, cancellationSource.Token);
// Make sure to pass the CancellationToken to all methods.
string content = await response.Content.ReadAsStringAsync(cancellationSource.Token);