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); }
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); }
/// <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 <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); }
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); }