コード例 #1
0
ファイル: Utils.cs プロジェクト: usaweili/V2RayGCon
        public static long VisitWebPageSpeedTest(string url = "https://www.google.com", int port = -1)
        {
            var timeout = Str2Int(StrConst("SpeedTestTimeout"));

            Lib.Utils.SupportProtocolTLS12();
            WebClient wc = new TimedWebClient
            {
                Encoding = System.Text.Encoding.UTF8,
                Timeout  = timeout * 1000,
            };

            long elasped = 0;

            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 { }
            wc.Dispose();

            return(elasped);
        }
コード例 #2
0
        public static string Fetch(string url, int timeout = -1)
        {
            var html = string.Empty;

            using (WebClient wc = new TimedWebClient
            {
                Encoding = System.Text.Encoding.UTF8,
                Timeout = timeout,
            })
            {
                /* 如果用抛出异常的写法
                 * task中调用此函数时
                 * 会弹出用户未处理异常警告
                 */
                try
                {
                    html = wc.DownloadString(url);
                }
                catch { }
            }
            return(html);
        }