question

briankeane-3259 avatar image
0 Votes"
briankeane-3259 asked TianyuSun-MSFT edited

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

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");

         }
     }
 }
dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tested in C++ (with WinHTTP) and I don't get this problem (that I have too in C#)
As I'm in France, I get first a "HTTP/1.1 302 Moved Temporarily" to "https://www.microsoft.com/fr-fr/"
Then I add the flag INTERNET_DEFAULT_HTTPS_PORT and "/fr-fr/" to the request and I get a normal "HTTP/1.1 200 OK" response, without any timeout/cancellation


1 Vote 1 ·

Thanks I will try this

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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;
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

briankeane-3259 avatar image
0 Votes"
briankeane-3259 answered karenpayneoregon commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

we need more than just the string

Yes I realize that, as mentioned, Microsoft tags on the language

For me
https://www.microsoft.com/ en-us/

0 Votes 0 ·