Exemple #1
0
        private PingCollectorResult PingHTTP()
        {
            PingCollectorResult result = new PingCollectorResult();
            string lastStep            = "[Create WebClientEx]";

            result.Success         = false;
            result.PingTime        = -1;
            result.ResponseDetails = "";
            try
            {
                lastStep = "[HTTPSSetup]";
                if (Address.ToLower().StartsWith("https://"))
                {
                    System.Net.ServicePointManager.Expect100Continue = true;
                    if (HttpsSecurityProtocol == "Tls")
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    }
                    else if (HttpsSecurityProtocol == "Tls11")
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11;
                    }
                    else if (HttpsSecurityProtocol == "Tls12")
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
                    }
                    else
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
                    }
                    //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
                    if (IgnoreInvalidHTTPSCerts)
                    {
                        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }
                    }
                    ;
                    else
                    {
                        System.Net.ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
                    }
                }

                Stopwatch sw = new Stopwatch();
                using (WebClientEx wc = new WebClientEx())
                {
                    wc.Timeout = (TimeOutMS * 1000);
                    if (HttpProxyServer.Length > 0)
                    {
                        System.Net.WebProxy     proxy       = null;
                        System.Net.ICredentials credentials = null;
                        if (HttpProxyUserName.Length == 0)
                        {
                            credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                            proxy       = new System.Net.WebProxy(HttpProxyServer, true, null, credentials);
                            proxy.UseDefaultCredentials = true;
                        }
                        else
                        {
                            if (HttpProxyUserName.Contains('\\'))
                            {
                                string domain   = HttpProxyUserName.Split('\\')[0];
                                string userName = HttpProxyUserName.Split('\\')[1];
                                credentials = new System.Net.NetworkCredential(userName, HttpProxyPassword, domain);
                            }
                            else
                            {
                                credentials = new System.Net.NetworkCredential(HttpProxyUserName, HttpProxyPassword);
                            }
                            proxy = new System.Net.WebProxy(HttpProxyServer, true, null, credentials);
                            proxy.UseDefaultCredentials = false;
                            proxy.Credentials           = credentials;
                        }
                        wc.Proxy = proxy;
                    }
                    else
                    {
                        wc.Proxy = null;
                    }
                    wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);

                    if (HttpHeaderUserName.Trim().Length > 0)
                    {
                        wc.UseDefaultCredentials = false;
                        wc.Credentials           = new System.Net.NetworkCredential(HttpHeaderUserName, HttpHeaderPassword);
                    }
                    else
                    {
                        wc.UseDefaultCredentials = true;
                    }

                    sw.Start();
                    lastStep = "[OpenRead]";
                    using (System.IO.Stream webRequest = wc.OpenRead(Address))
                    {
                        int           chars       = 0;
                        StringBuilder docContents = new StringBuilder();
                        lastStep = "[CanRead]";
                        if (webRequest.CanRead)
                        {
                            lastStep = "[ReadByte]";
                            int readByte = webRequest.ReadByte();
                            while (readByte != -1) // && chars < 256)
                            {
                                chars++;
                                docContents.Append(Char.ConvertFromUtf32(readByte));
                                readByte = webRequest.ReadByte();

                                if (sw.ElapsedMilliseconds > TimeOutMS)
                                {
                                    break;
                                }
                            }
                            if (chars > 1)
                            {
                                result.ResponseContent = docContents.ToString();
                                result.ResponseDetails = "Characters returned:" + chars.ToString();
                            }
                        }
                        else
                        {
                            throw new Exception("Could not read web request stream");
                        }
                    }
                    sw.Stop();
                }
                result.PingTime = (int)sw.ElapsedMilliseconds;
                if (sw.ElapsedMilliseconds < TimeOutMS)
                {
                    result.Success = true;
                }
                else
                {
                    result.Success = false;
                }
            }
            catch (Exception ex)
            {
                result.Success  = false;
                result.PingTime = -1;

                if (ex is System.Net.WebException)
                {
                    if (((System.Net.WebException)ex).Response != null && ((System.Net.WebException)ex).Response is System.Net.HttpWebResponse)
                    {
                        System.Net.HttpWebResponse httpResp = ((System.Net.HttpWebResponse)((System.Net.WebException)ex).Response);
                        if (httpResp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            result.ResponseDetails = string.Format("The URL '{0}' requires authorization to connect - i.e. Username/Password", Address);
                        }
                        else
                        {
                            result.ResponseDetails = string.Format("The URL '{0}' returned an HTTP error: {1}", httpResp);
                        }
                    }
                    else
                    {
                        result.ResponseDetails = lastStep + " " + ex.Message;
                    }
                }
                else
                {
                    if (ex.InnerException != null)
                    {
                        result.ResponseDetails = lastStep + " " + ex.InnerException.Message;
                    }
                    else
                    {
                        result.ResponseDetails = lastStep + " " + ex.Message;
                    }
                }
            }
            return(result);
        }
        private PingCollectorResult PingHTTP()
        {
            PingCollectorResult result = new PingCollectorResult();
            string lastStep            = "[Create WebClientEx]";

            result.Success         = false;
            result.PingTime        = -1;
            result.ResponseDetails = "";
            try
            {
                lastStep = "[HTTPSSetup]";
                if (Address.ToLower().StartsWith("https://"))
                {
                    System.Net.ServicePointManager.Expect100Continue = true;
                    System.Net.ServicePointManager.SecurityProtocol  = System.Net.SecurityProtocolType.Ssl3;
                    if (IgnoreInvalidHTTPSCerts)
                    {
                        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }
                    }
                    ;
                    else
                    {
                        System.Net.ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; // = delegate { return true; };
                    }
                }

                Stopwatch sw = new Stopwatch();
                using (WebClientEx wc = new WebClientEx())
                {
                    wc.Timeout = (TimeOutMS * 1000);
                    wc.UseDefaultCredentials = true;
                    if (HttpProxyServer.Length > 0)
                    {
                        wc.Proxy             = new System.Net.WebProxy(HttpProxyServer);
                        wc.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                    }
                    else
                    {
                        wc.Proxy = null;
                    }
                    wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
                    sw.Start();

                    lastStep = "[OpenRead]";
                    using (System.IO.Stream webRequest = wc.OpenRead(Address))
                    {
                        int chars = 0;
                        lastStep = "[CanRead]";
                        if (webRequest.CanRead)
                        {
                            lastStep = "[ReadByte]";
                            int readByte = webRequest.ReadByte();
                            while (readByte != -1) // && chars < 256)
                            {
                                readByte = webRequest.ReadByte();
                                chars++;
                                if (sw.ElapsedMilliseconds > TimeOutMS)
                                {
                                    break;
                                }
                            }
                            result.ResponseDetails = "Characters returned:" + chars.ToString();
                        }
                        else
                        {
                            throw new Exception("Could not read web request stream");
                        }
                    }
                    sw.Stop();
                }
                result.PingTime = (int)sw.ElapsedMilliseconds;
                if (sw.ElapsedMilliseconds < TimeOutMS)
                {
                    result.Success = true;
                }
                else
                {
                    result.Success         = false;
                    result.ResponseDetails = "Operation timed out!\r\n" + result.ResponseDetails;
                }
            }
            catch (Exception ex)
            {
                result.Success  = false;
                result.PingTime = -1;
                if (ex.InnerException != null)
                {
                    result.ResponseDetails = lastStep + " " + ex.InnerException.Message;
                }
                else
                {
                    result.ResponseDetails = lastStep + " " + ex.Message;
                }
            }
            return(result);
        }