程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> .net實現ping的實例代碼

.net實現ping的實例代碼

編輯: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);
            }

        }

    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved