服务器之家

服务器之家 > 正文

.net实现ping的实例代码

时间:2019-11-29 14:15     来源/作者:ASP.NET教程网

代码如下:


class ServicePinger 
    { 
        private static readonly ILog log = LogManager.GetLogger(typeof(ServicePinger)); 
        public ServicePinger(string siteName, string siteUrl, string serviceUrl) 
        { 
            if (siteName == null) 
                throw new ArgumentException("siteName can't be null"); 

            if (siteUrl == null) 
                throw new ArgumentException("siteUrl can't be null"); 

            if (serviceUrl == null) 
                throw new ArgumentException("serviceUrl can't be null"); 

            if (siteName.Length == 0) 
                throw new ArgumentException("siteName can't be empty"); 

            if (siteUrl.Length == 0) 
                throw new ArgumentException("siteUrl can't be empty"); 

            if (serviceUrl.Length == 0) 
                throw new ArgumentException("serviceUrl can't be empty"); 

            pingingSiteName = siteName; 
            pingingSiteUrl = siteUrl; 
            serviceUrlToPing = serviceUrl; 

  

        } 

        private string pingingSiteName = string.Empty; 
        private string pingingSiteUrl = string.Empty; 
        private string serviceUrlToPing = string.Empty; 
        private int timeoutInMilliseconds = 3000; 

  
        /// <summary> 
        /// Does the actual pinging of the service 
        /// </summary> 
        public void Ping() 
        { 
            try
            { 
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceUrlToPing); 
                request.Method = "POST"; 
                request.ContentType = "text/xml"; 
                request.Timeout = timeoutInMilliseconds; 
                request.Credentials = CredentialCache.DefaultNetworkCredentials; 

  
                Stream stream = (Stream)request.GetRequestStream(); 
                using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII)) 
                { 
                    writer.WriteStartDocument(); 
                    writer.WriteStartElement("methodCall"); 
                    writer.WriteElementString("methodName", "weblogUpdates.ping"); 
                    writer.WriteStartElement("params"); 
                    writer.WriteStartElement("param"); 
                    writer.WriteElementString("value", pingingSiteName); 
                    writer.WriteEndElement(); 
                    writer.WriteStartElement("param"); 
                    writer.WriteElementString("value", pingingSiteUrl); 
                    writer.WriteEndElement(); 
                    writer.WriteEndElement(); 
                    writer.WriteEndElement(); 
                } 

  
                request.GetResponse(); 
            } 
            catch (InvalidOperationException ex) 
            { 
                log.Error(ex); 
            } 
            catch (NotSupportedException ex) 
            { 
                log.Error(ex); 
            }

 

        } 

    }

 

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部