Ejemplo n.º 1
0
        public static long VisitWebPageSpeedTest(string url = "https://www.google.com", int port = -1)
        {
            var timeout = Str2Int(StrConst.SpeedTestTimeout);

            long elasped = long.MaxValue;

            using (WebClient wc = new Lib.Nets.TimedWebClient
            {
                Encoding = System.Text.Encoding.UTF8,
                Timeout = timeout * 1000,
            })
            {
                try
                {
                    if (port > 0)
                    {
                        wc.Proxy = new WebProxy("127.0.0.1", port);
                    }
                    Stopwatch sw = new Stopwatch();
                    sw.Reset();
                    sw.Start();
                    wc.DownloadString(url);
                    sw.Stop();
                    elasped = sw.ElapsedMilliseconds;
                }
                catch { }
            }

            return(elasped);
        }
Ejemplo n.º 2
0
        public static long VisitWebPageSpeedTest(string url = "https://www.google.com", int port = -1)
        {
            var timeout = Str2Int(StrConst.SpeedTestTimeout) * 1000;

            long elasped = long.MaxValue;

            try
            {
                using (WebClient wc = new Lib.Nets.TimedWebClient
                {
                    Encoding = System.Text.Encoding.UTF8,
                    Timeout = timeout,
                })
                {
                    if (port > 0)
                    {
                        wc.Proxy = new WebProxy("127.0.0.1", port);
                    }

                    var            result             = string.Empty;
                    AutoResetEvent speedTestCompleted = new AutoResetEvent(false);
                    wc.DownloadStringCompleted += (s, a) =>
                    {
                        try
                        {
                            result = a.Result;
                        }
                        catch { }
                        speedTestCompleted.Set();
                    };

                    Stopwatch sw = new Stopwatch();
                    sw.Reset();
                    sw.Start();
                    wc.DownloadStringAsync(new Uri(url));

                    // 收到信号为True
                    if (!speedTestCompleted.WaitOne(timeout))
                    {
                        wc.CancelAsync();
                        return(elasped);
                    }
                    sw.Stop();
                    if (!string.IsNullOrEmpty(result))
                    {
                        elasped = sw.ElapsedMilliseconds;
                    }
                }
            }
            catch { }
            return(elasped);
        }
Ejemplo n.º 3
0
        public static string Fetch(string url, int timeout = -1)
        {
            var html = string.Empty;

            using (WebClient wc = new Lib.Nets.TimedWebClient
            {
                Encoding = System.Text.Encoding.UTF8,
                Timeout = timeout,
            })
            {
                /* 如果用抛出异常的写法
                 * task中调用此函数时
                 * 会弹出用户未处理异常警告
                 */
                try
                {
                    html = wc.DownloadString(url);
                }
                catch { }
            }
            return(html);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Download through http://127.0.0.1:proxyPort. Return string.Empty if sth. goes wrong.
        /// </summary>
        /// <param name="url">string</param>
        /// <param name="proxyPort">1-65535, other value means download directly</param>
        /// <param name="timeout">millisecond, if &lt;1 then use default value 30000</param>
        /// <returns>If sth. goes wrong return string.Empty</returns>
        public static string FetchThroughProxy(string url, int proxyPort, int timeout)
        {
            var html = string.Empty;

            using (WebClient wc = new Lib.Nets.TimedWebClient
            {
                Encoding = System.Text.Encoding.UTF8,
                Timeout = timeout,
            })
            {
                if (proxyPort > 0 && proxyPort < 65536)
                {
                    wc.Proxy = new WebProxy("127.0.0.1", proxyPort);
                }

                try
                {
                    html = wc.DownloadString(url);
                }
                catch { }
            }
            return(html);
        }
Ejemplo n.º 5
0
        public static string FetchThroughProxy(string url, int proxyPort)
        {
            var html = string.Empty;

            using (WebClient wc = new Lib.Nets.TimedWebClient
            {
                Encoding = System.Text.Encoding.UTF8,
                Timeout = 30 * 1000,
            })
            {
                wc.Proxy = new WebProxy("127.0.0.1", proxyPort);

                /* 如果用抛出异常的写法
                 * task中调用此函数时
                 * 会弹出用户未处理异常警告
                 */
                try
                {
                    html = wc.DownloadString(url);
                }
                catch { }
            }
            return(html);
        }