Exemplo n.º 1
0
        public static bool IsValidUrl(string url, bool needTestConn)
        {
            bool result = false;

            if (string.IsNullOrEmpty(url))
            {
                return(result);
            }
            if (url.ToLower().Trim() == "about:blank")
            {
                return(true);
            }
            HttpWebResponse httpWebResponse = null;

            try
            {
                string pattern = "^http[s]?://([\\w-]+\\.)*[\\w-]+(:[\\d]*)?(/.*)?$";
                if (Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase))
                {
                    if (!needTestConn)
                    {
                        return(true);
                    }
                    HttpWebRequest httpWebRequest = WebRequestFactory.GetHttpWebRequest(url);
                    httpWebRequest.Timeout          = 6000;
                    httpWebRequest.ReadWriteTimeout = 10000;
                    httpWebRequest.KeepAlive        = false;
                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    return(true);
                }
                return(result);
            }
            catch
            {
                return(false);
            }
            finally
            {
                httpWebResponse?.Close();
            }
        }
Exemplo n.º 2
0
 public static HttpWebRequest GetHttpWebRequest(string url)
 {
     return((HttpWebRequest)WebRequestFactory.GetWebRequest(url));
 }