您好,登錄后才能下訂單哦!
這篇文章主要介紹了.net如何實(shí)現(xiàn)ping,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
代碼如下:
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);
}
}
}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“.net如何實(shí)現(xiàn)ping”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。