/
T10测试网络连接.cs
34 lines (32 loc) · 1.02 KB
/
T10测试网络连接.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
namespace Try_More_On_IEnumerable
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
/// <summary>
/// https://dotnetfiddle.net/Widget/IBHOxI
/// </summary>
public class T10测试网络连接
{
public static async Task Run()
{
var httpClient = new HttpClient();
try
{
await Task.WhenAll(SendRequests());
Console.WriteLine("当前网络连接正常");
}
catch (Exception e)
{
Console.WriteLine("当前网络不正常,请检查网络连接");
}
IEnumerable<Task> SendRequests()
{
yield return Task.Run(() => httpClient.GetAsync("http://www.baidu.com"));
yield return Task.Run(() => httpClient.GetAsync("http://www.bing.com"));
yield return Task.Run(() => httpClient.GetAsync("http://www.taobao.com"));
}
}
}
}