Created
August 29, 2023 13:05
-
-
Save admir-live/03c35fe5bc8fcb3410046da0467db046 to your computer and use it in GitHub Desktop.
The key is to read the content and dispose of it properly.
This file contains 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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
public class Program | |
{ | |
private static readonly HttpClient client = new HttpClient(); | |
public static async Task Main(string[] args) | |
{ | |
var request = new HttpRequestMessage(HttpMethod.Get, "https://example.com/api/data"); | |
using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) | |
{ | |
response.EnsureSuccessStatusCode(); | |
// The key is to read the content and dispose of it properly. | |
using (var contentStream = await response.Content.ReadAsStreamAsync()) | |
{ | |
// Process the stream (e.g., read the content, deserialize, etc.) | |
// ... | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment