-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add code examples and update content for HTTP error handling #31780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1edd14
WIP
IEvangelist f944a63
Add code and example content to existing HTTP conceptual doc for requ…
IEvangelist faa84ac
Tweak wording
IEvangelist 3bd2f07
Clean up
IEvangelist 9e88743
Correct statement
IEvangelist 4638d62
Address feedback
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
docs/fundamentals/networking/snippets/httpclient/Program.Cancellation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| static partial class Program | ||
| { | ||
| static async Task WithCancellationAsync(HttpClient httpClient) | ||
| { | ||
| // <cancellation> | ||
| using var cts = new CancellationTokenSource(); | ||
| try | ||
| { | ||
| // Assuming: | ||
| // httpClient.Timeout = TimeSpan.FromSeconds(10) | ||
|
|
||
| using var response = await httpClient.GetAsync( | ||
| "http://localhost:5001/sleepFor?seconds=100", cts.Token); | ||
| } | ||
| catch (TaskCanceledException ex) when (cts.IsCancellationRequested) | ||
| { | ||
| // When the token has been canceled, it is not a timeout. | ||
| WriteLine($"Canceled: {ex.Message}"); | ||
| } | ||
| // </cancellation> | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
docs/fundamentals/networking/snippets/httpclient/Program.CancellationInnerTimeout.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| static partial class Program | ||
| { | ||
| static async Task WithCancellationAndInnerTimeoutAsync(HttpClient httpClient) | ||
| { | ||
| // <innertimeout> | ||
| try | ||
| { | ||
| // Assuming: | ||
| // httpClient.Timeout = TimeSpan.FromSeconds(10) | ||
|
|
||
| using var response = await httpClient.GetAsync( | ||
| "http://localhost:5001/sleepFor?seconds=100"); | ||
| } | ||
| catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException tex) | ||
| { | ||
| WriteLine($"Timed out: {ex.Message}, {tex.Message}"); | ||
| } | ||
| // </innertimeout> | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
docs/fundamentals/networking/snippets/httpclient/Program.CancellationStatusCode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| static partial class Program | ||
| { | ||
| static async Task WithCancellationAndStatusCodeAsync(HttpClient httpClient) | ||
| { | ||
| // <statuscode> | ||
| try | ||
| { | ||
| // Assuming: | ||
| // httpClient.Timeout = TimeSpan.FromSeconds(10) | ||
|
|
||
| using var response = await httpClient.GetAsync( | ||
| "http://localhost:5001/doesNotExist"); | ||
|
|
||
| response.EnsureSuccessStatusCode(); | ||
| } | ||
| catch (HttpRequestException ex) when (ex is { StatusCode: HttpStatusCode.NotFound }) | ||
| { | ||
| // Handle 404 | ||
| Console.WriteLine($"Not found: {ex.Message}"); | ||
| } | ||
| // </statuscode> | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
docs/fundamentals/networking/snippets/httpclient/Program.CancellationStream.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| static partial class Program | ||
| { | ||
| static async Task WithCancellationExtensionsAsync(HttpClient httpClient) | ||
| { | ||
| // <helpers> | ||
| try | ||
| { | ||
| // These extension methods will throw HttpRequestException | ||
| // with StatusCode set when the HTTP request status code isn't 2xx: | ||
| // | ||
| // GetByteArrayAsync | ||
| // GetStreamAsync | ||
| // GetStringAsync | ||
|
|
||
| using var stream = await httpClient.GetStreamAsync( | ||
| "https://localhost:5001/doesNotExists"); | ||
| } | ||
| catch (HttpRequestException ex) when (ex is { StatusCode: HttpStatusCode.NotFound }) | ||
| { | ||
| // Handle 404 | ||
| Console.WriteLine($"Not found: {ex.Message}"); | ||
| } | ||
| // </helpers> | ||
| } | ||
| } |
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.Delete.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.Get.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.GetFromJson.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.Head.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.Options.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.PostAsJson.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.PutAsJson.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/fundamentals/networking/snippets/httpclient/Program.Responses.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
docs/fundamentals/networking/snippets/httpclient/Program.ThrowHttpException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| static partial class Program | ||
| { | ||
| static async Task ThrowHttpRequestExceptionAsync(HttpClient httpClient) | ||
| { | ||
| // <throw> | ||
| try | ||
| { | ||
| using var response = await httpClient.GetAsync( | ||
| "https://localhost:5001/doesNotExists"); | ||
|
|
||
| // Throw for anything higher than 400. | ||
| if (response is { StatusCode: >= HttpStatusCode.BadRequest }) | ||
| { | ||
| throw new HttpRequestException( | ||
| "Something went wrong", inner: null, response.StatusCode); | ||
| } | ||
| } | ||
| catch (HttpRequestException ex) when (ex is { StatusCode: HttpStatusCode.NotFound }) | ||
| { | ||
| Console.WriteLine($"Not found: {ex.Message}"); | ||
| } | ||
| // </throw> | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.