首頁 > php教程 > php手册 > 主體

net实现ping的方法

WBOY
發布: 2016-07-06 13:30:18
原創
1390 人瀏覽過

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 ca

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);
            }
 
 
 
        }
 
    }
登入後複製

  


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板