主页 > 知识库 > .net实现ping的实例代码

.net实现ping的实例代码

热门标签:福州人工智能电销机器人加盟 怎样给陕西地图标注颜色 ai电销机器人对贷款有帮助吗 云狐人工智能电话机器人 地图标注多少钱一张 400电话办理信任翰诺科技 宿迁智能外呼系统排名 电销机器人 数据 广州销售外呼系统定制

复制代码 代码如下:

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的实例代码》,本文关键词  .net,实现,ping,的,实例,代码,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《.net实现ping的实例代码》相关的同类信息!
  • 本页收集关于.net实现ping的实例代码的相关信息资讯供网民参考!
  • 推荐文章