Skip to content

Instantly share code, notes, and snippets.

@daanl
Created October 5, 2016 09:26
Show Gist options
  • Save daanl/92859fa24d393dcbbf2b23581ea92b9c to your computer and use it in GitHub Desktop.
Save daanl/92859fa24d393dcbbf2b23581ea92b9c to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
Run().Wait();
}
public static async Task Run()
{
var uri = new Uri("https://myUrl.com");
var baseUri = new Uri($"{uri.Scheme}://{uri.Host}");
var servicePoint = ServicePointManager.FindServicePoint(baseUri);
servicePoint.ConnectionLeaseTimeout = 0;
ServicePointManager.DnsRefreshTimeout = 1;
using (var httpClient = new HttpClient())
{
httpClient.Timeout = TimeSpan.FromSeconds(5);
// result = OK
var dns = Dns.GetHostAddresses(uri.Host);
var result = await httpClient.GetAsync(uri.AbsoluteUri);
var content = await result.Content.ReadAsStringAsync();
var test = "1231";
// dns changed
await Task.Delay(TimeSpan.FromSeconds(5));
var dns1 = Dns.GetHostAddresses(uri.Host);
var result1 = await httpClient.GetAsync(uri.AbsoluteUri);
var content1 = await result.Content.ReadAsStringAsync();
}
await Task.Delay(TimeSpan.FromSeconds(5));
var stopwatch = Stopwatch.StartNew();
try
{
var reuse = new HttpClient();
while (true)
{
var dns2 = Dns.GetHostAddresses(uri.Host);
var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(5);
var result2 = await httpClient.GetAsync(uri.AbsoluteUri);
var content2 = await result2.Content.ReadAsStringAsync();
Console.WriteLine($"Finished request dns: {uri.Host}-{dns2.First()}: {stopwatch.Elapsed}");
await Task.Delay(TimeSpan.FromMilliseconds(1500));
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception after: {stopwatch.Elapsed}");
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment