markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / SytemNet.cs @ ff4b1e6e
이력 | 보기 | 이력해설 | 다운로드 (2.57 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 | public static bool Ping(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 | ff4b1e6e | taeseongkim | Client.Timeout = new TimeSpan(0, 0, 10); |
37 | |||
38 | var task = Client.GetAsync(uri); |
||
39 | |||
40 | if (!task.IsFaulted) |
||
41 | 06f13e11 | taeseongkim | { |
42 | ff4b1e6e | taeseongkim | System.Net.Http.HttpResponseMessage responseMessage = task.Result; |
43 | System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
||
44 | switch (StatusCode) |
||
45 | { |
||
46 | 06f13e11 | taeseongkim | |
47 | ff4b1e6e | taeseongkim | case System.Net.HttpStatusCode.Accepted: |
48 | case System.Net.HttpStatusCode.OK: |
||
49 | result = true; |
||
50 | break; |
||
51 | } |
||
52 | 06f13e11 | taeseongkim | } |
53 | } |
||
54 | } |
||
55 | ff4b1e6e | taeseongkim | else |
56 | { |
||
57 | result = false; |
||
58 | } |
||
59 | 06f13e11 | taeseongkim | } |
60 | catch (AggregateException ae) |
||
61 | { |
||
62 | foreach (var e in ae.InnerExceptions) |
||
63 | { |
||
64 | // Handle the custom exception. |
||
65 | if (e is CustomException) |
||
66 | { |
||
67 | Console.WriteLine(e.Message); |
||
68 | } |
||
69 | // Rethrow any other exception. |
||
70 | else |
||
71 | { |
||
72 | ff4b1e6e | taeseongkim | Console.WriteLine(strUri + " Connection Error"); |
73 | 06f13e11 | taeseongkim | } |
74 | } |
||
75 | } |
||
76 | catch (Exception) |
||
77 | { |
||
78 | |||
79 | } |
||
80 | |||
81 | return result; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public class CustomException : Exception |
||
86 | { |
||
87 | public CustomException(String message) : base(message) |
||
88 | { } |
||
89 | } |
||
90 | } |