Copy codeThe 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;
/// <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", pingingingsitename );
Writer. WriteEndElement ();
Writer. WriteStartElement ("param ");
Writer. WriteElementString ("value", pingingingsiteurl );
Writer. WriteEndElement ();
Writer. WriteEndElement ();
Writer. WriteEndElement ();
}
Request. GetResponse ();
}
Catch (InvalidOperationException ex)
{
Log. Error (ex );
}
Catch (NotSupportedException ex)
{
Log. Error (ex );
}
}
}