Get the App
SLTechnology News&Howtos  ›  Development  › 

.net how to implement ping

Shulou Source: shulou.com Published: 2022-06-03 08:42:16 10月01日 Update

This article mainly introduces how. Net to achieve ping, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The code is as follows:

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

/ / /

/ Does the actual pinging of the service

/ / /

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)

}

}

}

Thank you for reading this article carefully. I hope the article ".net how to achieve ping" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Articles code value interests at the same time more friends knowledge editing industry information information channels channels reference learning help support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei Linux Xiaomi Shulou Tech Info