Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active May 17, 2018
Embed
What would you like to do?
HttpClient Example 2
namespace HttpClientTest
{
using System;
using System.Net.Http;
class Program
{
private static readonly int _connections = 1000;
private static readonly HttpClient _httpClient = new HttpClient();
private static void Main()
{
TestHttpClientWithStaticInstance();
TestHttpClientWithUsing();
}
private static void TestHttpClientWithUsing()
{
try
{
for (var i = 0; i < _connections; i++)
{
using (var httpClient = new HttpClient())
{
var result = httpClient.GetAsync(new Uri("http://bing.com")).Result;}
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private static void TestHttpClientWithStaticInstance()
{
try
{
for (var i = 0; i < _connections; i++)
{
var result = _httpClient.GetAsync(new Uri("http://bing.com")).Result;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment