C# 如何获取web服务的状态
如何使用C#获取web服务的状态?它是成功完成的、失败的还是像这样挂起的。首先我发现了这个问题 你应该试试这个代码C# 如何获取web服务的状态,c#,asp.net,web-services,C#,Asp.net,Web Services,如何使用C#获取web服务的状态?它是成功完成的、失败的还是像这样挂起的。首先我发现了这个问题 你应该试试这个代码 System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("myService"); return sc.Status public static string PingHost(string args) { Ht
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("myService");
return sc.Status
public static string PingHost(string args)
{
HttpWebResponse res = null;
try
{
// Create a request to the passed URI.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(args);
req.Credentials = CredentialCache.DefaultNetworkCredentials;
// Get the response object.
res = (HttpWebResponse)req.GetResponse();
return "Service Up";
}
catch (Exception e)
{
MessageBox.Show("Source : " + e.Source, "Exception Source", MessageBoxButtons.OK);
MessageBox.Show("Message : " + e.Message, "Exception Message", MessageBoxButtons.OK);
return "Host Unavailable";
}
}
你应该检查这个代码
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("myService");
return sc.Status
public static string PingHost(string args)
{
HttpWebResponse res = null;
try
{
// Create a request to the passed URI.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(args);
req.Credentials = CredentialCache.DefaultNetworkCredentials;
// Get the response object.
res = (HttpWebResponse)req.GetResponse();
return "Service Up";
}
catch (Exception e)
{
MessageBox.Show("Source : " + e.Source, "Exception Source", MessageBoxButtons.OK);
MessageBox.Show("Message : " + e.Message, "Exception Message", MessageBoxButtons.OK);
return "Host Unavailable";
}
}
你也应该看看
如果您在同步模式下调用Web服务,则获取状态没有问题
如果在异步模式下调用Web服务,则应设置回调函数,并在返回给该回调函数的结果中跟踪Web服务状态。我尝试了您的解决方案,但出现异常-在计算机“computername”上未找到服务“myservice”。
private void button1_Click(object sender, EventArgs e)
{
var url = "servicsURL";
try
{
var myRequest = (HttpWebRequest)WebRequest.Create(url);
NetworkCredential networkCredential = new NetworkCredential("UserName", "password","domain");
// Associate the 'NetworkCredential' object with the 'WebRequest' object.
myRequest.Credentials = networkCredential;
var response = (HttpWebResponse)myRequest.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// it's at least in some way responsive
// but may be internally broken
// as you could find out if you called one of the methods for real
MessageBox.Show(string.Format("{0} Available", url));
}
else
{
// well, at least it returned...
MessageBox.Show(string.Format("{0} Returned, but with status: {1}", url, response.StatusDescription));
}
}
catch (Exception ex)
{
// not available at all, for some reason
MessageBox.Show(string.Format("{0} unavailable: {1}", url, ex.Message));
}
}