繁体   English   中英

如何获取网络服务状态

[英]How to get status of web service

如何使用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));
        }

    }

首先,我发现了此问题Web服务的数据库/网站状态

您应该尝试使用此代码;

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服务,则应设置一个回调函数并在返回给该回调函数的结果中跟踪Web服务状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM