C# 서버 연결 모니터링

3313 단어 WebServiceC#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace SYS_TEST.OtherClass
{
    // 
    public class ServiceMonitor
    {
        private string strMsg = "";

        /// 
        ///  (IP / )
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool TryConnectToServer(string address, int port, string type, int timeout)
        {
            try
            {
                if (type == "IP")
                {
                    Ping pingsend = new Ping();
                    PingReply pingreply = pingsend.Send(address, timeout);
                    if (pingreply.Status != IPStatus.Success)
                    {
                        strMsg = " :" + pingreply.Status.ToString();
                        return false;
                    }
                }
                if (type == "HTTP")
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);// 
                    WebRequest myRequest = WebRequest.Create(address);
                    myRequest.Timeout = timeout;
                    WebResponse myResponse = myRequest.GetResponse();
                    myResponse.Close();
                }
            }
            catch (Exception e)
            {
                strMsg = " , :" + e.Message;
                return false;
            }
            return true;
        }
        /// 
        /// FTP 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool TryConnectToFtp(string address, int port, int timeout, string ftpUserName, string ftpUserPwd)
        {
            try
            {
                FtpWebRequest ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(address));
                ftprequest.Credentials = new NetworkCredential(ftpUserName, ftpUserPwd);
                ftprequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                ftprequest.Timeout = timeout;
                FtpWebResponse ftpResponse = (FtpWebResponse)ftprequest.GetResponse();
                ftpResponse.Close();
            }
            catch (Exception e)
            {
                strMsg = " , :" + e.Message;
                return false;
            }
            return true;
        }
        /// 
        ///  
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        protected bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   //    
            return true;
        }
    }
}

좋은 웹페이지 즐겨찾기