gpt4 book ai didi

c# - 如何获取网络服务的状态

转载 作者:太空宇宙 更新时间:2023-11-03 14:20:17 25 4
gpt4 key购买 nike

如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com