检查URL、或者虚拟目录是否可以访问

        //检查虚拟目录是否可以访问
        public static bool Cmd(string cmdLine)
        {
            using (var process = new Process
            {
                StartInfo =
                {
                    FileName = "cmd.exe",
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,
                    RedirectStandardError = true
                }
            })
            {
                process.Start();
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine(cmdLine);
                process.StandardInput.WriteLine("exit");

                Debug.WriteLine(process.StandardOutput.ReadToEnd());

                String errorMessage = process.StandardError.ReadToEnd();
                process.WaitForExit();

                if (String.IsNullOrEmpty(errorMessage))
                {
                    return true;
                }

                Debug.WriteLine(errorMessage);
                return false;
            }
        }

 

        //检查链接是否可以访问
        public static bool CheckUrlVisit(string url)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    resp.Close();
                    return true;
                }
            }
            catch (WebException webex)
            {
                return false;
            }
            return false;
        }

 

posted on 2013-05-15 17:23  fish_1949  阅读(275)  评论(0编辑  收藏  举报

导航