香港空间的确很坑,网站可用性只有72.37%
今天测了下自己买的香港空间,每隔1分钟获取下网站内容,试了76次,只有55次成功拿到网页。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace getsites { class Program { static void Main(string[] args) { int i = 1; while (true) { Console.WriteLine("Round:"+i.ToString()+Environment.NewLine); string result = DownLoad_HTML.GetHtml("http://www.xxx.com/"); System.IO.File.AppendAllText(i.ToString() + ".html", result); Thread.Sleep(60 * 1000); i++; } } } public static class DownLoad_HTML { private static int FailCount = 0; //记录下载失败的次数 public static string GetHtml(string url) //传入要下载的网址 { string str = string.Empty; try { System.Net.WebRequest request = System.Net.WebRequest.Create(url); request.Timeout = 10000; //下载超时时间 request.Headers.Set("Pragma", "no-cache"); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream streamReceive = response.GetResponseStream(); Encoding encoding = Encoding.GetEncoding("gb2312");//utf-8 网页文字编码 System.IO.StreamReader streamReader = new System.IO.StreamReader(streamReceive, encoding); str = streamReader.ReadToEnd(); streamReader.Close(); } catch (Exception ex) { FailCount++; if (FailCount > 5) { System.IO.File.AppendAllText("error.log", "已下载失败" + FailCount + "次"); } else { str = GetHtml(url); } } FailCount = 0; //如果能执行到这一步就表示下载终于成功了 return str; } } }