markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / SytemNet.cs @ ff4b1e6e
이력 | 보기 | 이력해설 | 다운로드 (2.57 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Net.NetworkInformation; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
|
8 |
namespace Markus.Service.Extensions |
9 |
{ |
10 |
|
11 |
/// <summary> |
12 |
/// 네트워크 관련 확장코드 |
13 |
/// </summary> |
14 |
public static class SytemNet |
15 |
{ |
16 |
/// <summary> |
17 |
/// 웹uri 활성화 상태 체크 |
18 |
/// </summary> |
19 |
/// <param name="strUri"></param> |
20 |
/// <returns></returns> |
21 |
public static bool Ping(string strUri) |
22 |
{ |
23 |
bool result = false; |
24 |
|
25 |
try |
26 |
{ |
27 |
Uri uri = new Uri(strUri); |
28 |
|
29 |
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 |
{ |
36 |
Client.Timeout = new TimeSpan(0, 0, 10); |
37 |
|
38 |
var task = Client.GetAsync(uri); |
39 |
|
40 |
if (!task.IsFaulted) |
41 |
{ |
42 |
System.Net.Http.HttpResponseMessage responseMessage = task.Result; |
43 |
System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
44 |
switch (StatusCode) |
45 |
{ |
46 |
|
47 |
case System.Net.HttpStatusCode.Accepted: |
48 |
case System.Net.HttpStatusCode.OK: |
49 |
result = true; |
50 |
break; |
51 |
} |
52 |
} |
53 |
} |
54 |
} |
55 |
else |
56 |
{ |
57 |
result = false; |
58 |
} |
59 |
} |
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 |
Console.WriteLine(strUri + " Connection Error"); |
73 |
} |
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 |
} |