测试与网页的连接

时间:2014-10-06 14:53:31

标签: c# http web

如果某个特定网站可用,我正试图在我的代码中进行测试

 public override HealthCheckResult DoHealthCheck()
        {


            // Create a request using a URL that can receive a post. 
            var request = (HttpWebRequest)WebRequest.Create(_healthCheckUrl); 
            // Set the Method property of the request to POST.
            request.Method = "GET";
            request.Timeout = HealthCheckConfiguration.Timeout * 1000;\\Very Very large timeout
            // Set the ContentType property of the WebRequest.
            request.ContentType = "text/xml; encoding='utf-8'";
            // Get the response.
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                //var response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return HealthCheckResult.Success;

                }
            }
            catch (WebException)
            {
                  return HealthCheckResult.Timeout;
            }
            return HealthCheckResult.Failed;
        }

有时我会得到HttpStatusCode.OK,但大多数时候我都会收到超时异常。 如果我只是复制粘贴Internet Explorer中的_healthCheckUrl字符串,页面总是很快成功加载。我不明白为什么在代码中我会得到这么多超时。

提前致谢。

0 个答案:

没有答案