I get a cancellation exception when trying to www.microsoft.com in code

brian keane 1 Reputation point
2022-05-24T09:15:20.043+00:00

We have code in the field that tries to access www.microsoft.com through code. In the past 6 weeks or so it has been timing out after 100 seconds. If I try it again without closing the app, it works. Do you know if something changed on the side? It was working fine before. This GetAsync() call fails on the first try:

With this main method in a console app, the error can be reproduced. Passing in a parameter of 2 from the command line, the first attempt will fail, the second will succeed.

internal class Program
{
static void Main(string[] args)
{
var count = args?.Length > 0 ? int.Parse(args[0]) : 1;
for (int i = 0; i < count; i++)
{
var sw = new Stopwatch();
Console.WriteLine($"Request attempt number {i}");
sw.Start();
try
{
using var client = new HttpClient();
using var response = client.GetAsync("http://www.microsoft.com", new CancellationToken()).Result;
Console.WriteLine($"Date: {response.Headers.Date.GetValueOrDefault()}");
Console.WriteLine($"Status code: {response.StatusCode}");
Dictionary<string, string> ss
= response.Headers.ToDictionary(a => a.Key, a => string.Join(";", a.Value));
foreach (var entry in ss)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
}
catch (AggregateException ae)
{
foreach (var innerException in ae.Flatten().InnerExceptions)
{
Console.WriteLine(
$"Exception type: {innerException.GetType()}, message - {innerException.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception type: {ex.GetType()}, message - {ex.Message}");
}
sw.Stop();
Console.WriteLine($"Total elapsed time: {sw.ElapsedMilliseconds / 1000} seconds");

        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,205 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2022-05-24T10:03:32.33+00:00

    I had the following hanging around, works fine.

    public static async Task<string> MakeRequestWithWhenFilter(string address = "https://www.microsoft.com/")
    {
        var client = new HttpClient();
        var streamTask = client.GetStringAsync(address);
        try
        {
            var responseText = await streamTask;
            return responseText;
        }
        catch (HttpRequestException e) when (e.Message.Contains("301"))
        {
            return "Site Moved";
        }
        catch (HttpRequestException e) when (e.Message.Contains("404"))
        {
            return "Page Not Found";
        }
        catch (HttpRequestException e)
        {
            return e.Message;
        }
    }
    
    0 comments No comments

  2. brian keane 1 Reputation point
    2022-05-24T10:27:00.34+00:00

    We check the headers of the response so we need more than just the string. Also the code is out in the field. Unfortunately the previous engineer hard coded www.microsoft.com so we can not just change it but it works with other like time.google.com. Just in the past 6 weeks or so it started hanging so I am wondering if Microsoft put in some type security checks or something? It didn't timeout before. Thanks