You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs
+22-7Lines changed: 22 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,30 @@ class HttpCompletionOptionSnippets
9
9
publicstaticasyncTaskRunAsync()
10
10
{
11
11
//<SnippetHttpCompletionOption>
12
-
varhttpClient=newHttpClient();
12
+
usingvarhttpClient=newHttpClient();
13
13
httpClient.Timeout=TimeSpan.FromSeconds(30);
14
-
using(varresponse=awaithttpClient.GetAsync("http://localhost:12345/",HttpCompletionOption.ResponseHeadersRead))// 30-second timeout but ONLY up until past the headers
15
-
{
16
-
// Do other stuff that doesn't rely on the content first, like status code validation
17
-
response.EnsureSuccessStatusCode();
14
+
httpClient.MaxResponseContentBufferSize=1_000;// This will be ignored
18
15
19
-
varcontent=awaitresponse.Content.ReadAsStringAsync();// NO TIMEOUT
20
-
}
16
+
// Because we're specifying the ResponseHeadersRead option,
17
+
// the 30-second timeout applies only up until the headers are received.
18
+
// It does not affect future operations that interact with the response content.
Copy file name to clipboardExpand all lines: xml/System.Net.Http/HttpCompletionOption.xml
+5-1Lines changed: 5 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,11 @@
40
40
<formattype="text/markdown"><![CDATA[
41
41
42
42
## Remarks
43
-
The <xref:System.Net.Http.HttpCompletionOption> value effectively affects the scope of the timeout specified in the <xref:System.Net.Http.HttpClient> operation options when reading a response. The timeout on the <xref:System.Net.Http.HttpClient> always applies on the relevant invoked methods up until the point where those methods complete/return. Crucially, when using the <xref:System.Net.Http.HttpCompletionOption.ResponseHeadersRead> option, the timeout applies only up to where the headers end and the content starts. The content reading operation needs to be timed out separately in case the server promptly returns the status line and headers but takes too long to return the content. Below is an example illustrating this point:
43
+
44
+
> [!WARNING]
45
+
> The <xref:System.Net.Http.HttpCompletionOption> value affects the scope of the timeout specified in the <xref:System.Net.Http.HttpClient> options when reading a response. The timeout on the <xref:System.Net.Http.HttpClient> always applies on the relevant invoked methods up until the point where those methods complete/return. Crucially, when using the <xref:System.Net.Http.HttpCompletionOption.ResponseHeadersRead> option, the timeout applies only up to where the headers end and the content starts. The content reading operation needs to be timed out separately in case the server promptly returns the status line and headers but takes too long to return the content.
46
+
> This consideration also applies to the <xref:System.Net.Http.HttpClient.MaxResponseContentBufferSize%2A> property. The limit is only enforced when using <xref:System.Net.Http.HttpCompletionOption.ResponseContentRead>.
0 commit comments