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

标签 c# asp.net web-services

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

相关文章:

c# - 下载大型 Azure 存储 Blob 的最快方法?

c# - 不能有两个具有相同签名但不同通用约束的方法

c# - 为什么 `i += i++` 在 C 中是 1 而在 C# 中是 0?

c# - 显示/隐藏 Div

java - SpringBoot WebService客户端: MultipartFile and String parameters

c# - 未返回对通过 AddFromTemplate() 创建的项目项的引用

c# - 是否存在 OnDeserialized 事件或类似事件以便我可以确定某个类已被反序列化?

asp.net - Swagger w/ASP.NET v5 Azure Api 应用程序

java - 使用 spring mvc 和 hibernate Rest 服务从数据库保存和检索图像

java - 简单 HTTP 请求与 Web 服务