Skip to content

Instantly share code, notes, and snippets.

@admir-live
Created August 29, 2023 13:05
Show Gist options
  • Save admir-live/03c35fe5bc8fcb3410046da0467db046 to your computer and use it in GitHub Desktop.
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.
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