如何使用 C# 获取 Web 服务的状态?无论是成功完成、失败还是像这样挂起。
最佳答案
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));
}
}
关于c# - 如何获取网络服务的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5631029/