华域联盟 .Net .net实现ping的实例代码

.net实现ping的实例代码

复制代码 代码如下:

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);

            }

        }

    }

本文由 华域联盟 原创撰写:华域联盟 » .net实现ping的实例代码

转载请保留出处和原文链接:https://www.cnhackhy.com/53829.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部