프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / SytemNet.cs @ 218e5002

이력 | 보기 | 이력해설 | 다운로드 (2.46 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 async Task<bool> PingAsync(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, 1,0);
37

    
38
                        var message = await Client.GetAsync(uri);
39

    
40
                        System.Net.Http.HttpResponseMessage responseMessage = message;
41
                        System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode;
42
                        switch (StatusCode)
43
                        {
44

    
45
                            case System.Net.HttpStatusCode.Accepted:
46
                            case System.Net.HttpStatusCode.OK:
47
                                result = true;
48
                                break;
49
                        }
50
                    }
51
                }
52
                else
53
                {
54
                    result = false;
55
                }
56
            }
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
                        Console.WriteLine(strUri + " Connection Error");
70
                    }
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
}
클립보드 이미지 추가 (최대 크기: 500 MB)