markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / SytemNet.cs @ 06f13e11
이력 | 보기 | 이력해설 | 다운로드 (1.83 KB)
1 | 06f13e11 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | |||
7 | namespace Markus.Service.Extensions |
||
8 | { |
||
9 | public static class SytemNet |
||
10 | { |
||
11 | public static bool Ping(string uri) |
||
12 | { |
||
13 | bool result = false; |
||
14 | |||
15 | try |
||
16 | { |
||
17 | using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
||
18 | { |
||
19 | var task = Client.GetAsync(uri); |
||
20 | |||
21 | if (!task.IsFaulted) |
||
22 | { |
||
23 | System.Net.Http.HttpResponseMessage responseMessage = task.Result; |
||
24 | System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
||
25 | switch (StatusCode) |
||
26 | { |
||
27 | |||
28 | case System.Net.HttpStatusCode.Accepted: |
||
29 | case System.Net.HttpStatusCode.OK: |
||
30 | result = true; |
||
31 | break; |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | catch (AggregateException ae) |
||
37 | { |
||
38 | foreach (var e in ae.InnerExceptions) |
||
39 | { |
||
40 | // Handle the custom exception. |
||
41 | if (e is CustomException) |
||
42 | { |
||
43 | Console.WriteLine(e.Message); |
||
44 | } |
||
45 | // Rethrow any other exception. |
||
46 | else |
||
47 | { |
||
48 | Console.WriteLine(uri + " Connection Error"); |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 | catch (Exception) |
||
53 | { |
||
54 | |||
55 | } |
||
56 | |||
57 | return result; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | public class CustomException : Exception |
||
62 | { |
||
63 | public CustomException(String message) : base(message) |
||
64 | { } |
||
65 | } |
||
66 | } |