Skip to content

Instantly share code, notes, and snippets.

@jirolabo
Created March 27, 2019 12:58
Show Gist options
  • Save jirolabo/f4f2ee7f96cf9351444d55d0d8629314 to your computer and use it in GitHub Desktop.
Save jirolabo/f4f2ee7f96cf9351444d55d0d8629314 to your computer and use it in GitHub Desktop.
HttpClientFactory for console application
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApp1
{
// Install-Package Microsoft.Extensions.DependencyInjection
// Install-Package Microsoft.Extensions.Http
class HttpClientCoreSapmle
{
internal void Execute()
{
var httpClient = new ServiceCollection()
.AddHttpClient()
.BuildServiceProvider()
.GetService<IHttpClientFactory>()
.CreateClient();
var html = httpClient.GetStringAsync("https://twitter.com/ko_ro_chin").Result;
}
}
}
@robertlyson
Copy link

+1

@vfsdan
Copy link

vfsdan commented Jun 5, 2024

Nice simple example. Works in test project too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment