В некоторых частях мира эта функция всегда возвращает false, нам нужно установить прокси-сервер в Windows, чтобы эта функция возвращала true. Эта функция пытается создать HttpClient с поддержкой прокси, поэтому нам не нужно перезапускать приложение после установки прокси-сервера.
Кто-нибудь знает, что вызывает исключение и как его устранить, спасибо.
Код: Выделить всё
public static async Task TestInternetConnectivity()
{
try
{
Trace.Listeners.Add(new TextWriterTraceListener("C:\\Debug.log"));
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetService();
var client = httpClientFactory.CreateClient("ProxyAwareClient");
var response = await client.GetAsync($"http://www.msftconnecttest.com/connecttest.txt");
var result = await response.Content.ReadAsStringAsync();
if (!string.Equals(result, "Microsoft Connect Test"))
{
Trace.WriteLine($"Microsoft Connect Test result: false ");
Trace.Flush();
return false;
}
Trace.WriteLine($"Microsoft Connect Test result: true ");
Trace.Flush();
return true;
}
catch (Exception ex)
{
Trace.WriteLine($"Microsoft Connect Test result: false(Exception)");
Trace.WriteLine(ex.Message);
Trace.Flush();
return false;
}
}
Код: Выделить всё
public void ConfigureServices(ServiceCollection services)
{
//Proxy Aware HttpClient
services.AddHttpClient("ProxyAwareClient").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
UseProxy = true,
UseDefaultCredentials = true,
Proxy = null
});
}
Код: Выделить всё
Microsoft Connect Test result: false(Exception)
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (15.65.196.7:8080)
Код: Выделить всё
Microsoft Connect Test result: false(Exception)
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (www.msftconnecttest.com:80)

Подробнее здесь: https://stackoverflow.com/questions/798 ... httpclient
Мобильная версия