.net实现ping的实例代码
程序员文章站
2024-02-24 14:27:46
复制代码 代码如下:class servicepinger {  ...
复制代码 代码如下:
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);
}
}
}