markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / SytemNet.cs @ 974af55b
이력 | 보기 | 이력해설 | 다운로드 (2.46 KB)
1 | 06f13e11 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | ff4b1e6e | taeseongkim | using System.Net.NetworkInformation; |
5 | 06f13e11 | taeseongkim | using System.Text; |
6 | using System.Threading.Tasks; |
||
7 | |||
8 | namespace Markus.Service.Extensions |
||
9 | { |
||
10 | ff4b1e6e | taeseongkim | |
11 | /// <summary> |
||
12 | /// 네트워크 관련 확장코드 |
||
13 | /// </summary> |
||
14 | 06f13e11 | taeseongkim | public static class SytemNet |
15 | { |
||
16 | ff4b1e6e | taeseongkim | /// <summary> |
17 | /// 웹uri 활성화 상태 체크 |
||
18 | /// </summary> |
||
19 | /// <param name="strUri"></param> |
||
20 | /// <returns></returns> |
||
21 | 1ae729e4 | taeseongkim | public static async Task<bool> PingAsync(string strUri) |
22 | 06f13e11 | taeseongkim | { |
23 | bool result = false; |
||
24 | |||
25 | try |
||
26 | { |
||
27 | ff4b1e6e | taeseongkim | Uri uri = new Uri(strUri); |
28 | 06f13e11 | taeseongkim | |
29 | ff4b1e6e | taeseongkim | Ping pingSender = new Ping(); |
30 | PingReply reply = pingSender.Send(uri.Host); |
||
31 | |||
32 | if (reply.Status == IPStatus.Success) |
||
33 | { |
||
34 | using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
||
35 | 06f13e11 | taeseongkim | { |
36 | 1ae729e4 | taeseongkim | Client.Timeout = new TimeSpan(0, 1,0); |
37 | ff4b1e6e | taeseongkim | |
38 | 1ae729e4 | taeseongkim | var message = await Client.GetAsync(uri); |
39 | ff4b1e6e | taeseongkim | |
40 | 1ae729e4 | taeseongkim | System.Net.Http.HttpResponseMessage responseMessage = message; |
41 | System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
||
42 | switch (StatusCode) |
||
43 | 06f13e11 | taeseongkim | { |
44 | |||
45 | 1ae729e4 | taeseongkim | case System.Net.HttpStatusCode.Accepted: |
46 | case System.Net.HttpStatusCode.OK: |
||
47 | result = true; |
||
48 | break; |
||
49 | 06f13e11 | taeseongkim | } |
50 | } |
||
51 | } |
||
52 | ff4b1e6e | taeseongkim | else |
53 | { |
||
54 | result = false; |
||
55 | } |
||
56 | 06f13e11 | taeseongkim | } |
57 | catch (AggregateException ae) |
||
58 | { |
||
59 | foreach (var e in ae.InnerExceptions) |
||
60 | { |
||
61 | // Handle the custom exception. |
||
62 | if (e is CustomException) |
||
63 | { |
||
64 | Console.WriteLine(e.Message); |
||
65 | } |
||
66 | // Rethrow any other exception. |
||
67 | else |
||
68 | { |
||
69 | ff4b1e6e | taeseongkim | Console.WriteLine(strUri + " Connection Error"); |
70 | 06f13e11 | taeseongkim | } |
71 | } |
||
72 | } |
||
73 | catch (Exception) |
||
74 | { |
||
75 | |||
76 | } |
||
77 | |||
78 | return result; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | public class CustomException : Exception |
||
83 | { |
||
84 | public CustomException(String message) : base(message) |
||
85 | { } |
||
86 | } |
||
87 | } |