成长的悲伤在于有一天你会成为你曾经讨厌的人。人生短短数十载,最要紧的是满足自己,不是讨好他人。
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的实例代码请关注haodaima.com其它相关文章!