.net how to implement ping
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!