本文共 1941 字,大约阅读时间需要 6 分钟。
今天测了下自己买的香港空间,每隔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; } }}
有谁有可用性95%以上的香港空间,推荐下,不胜感激!
转载地址:http://alzxx.baihongyu.com/