HttpWebRequest 抓取页面异常处理办法
抓取页面异常处理办法
public static string GetHtmlTest(string URI) { string fullhtml = null; while (true) { try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI); req.Method = "GET"; req.UserAgent = "Opera/9.25 (Windows NT 6.0; U; en)"; req.KeepAlive = true; req.Timeout = 6000000; HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); if (resp.StatusCode != HttpStatusCode.OK) //如果服务器未响应,那么继续等待相应 continue; StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8); fullhtml = sr.ReadToEnd().Trim(); resp.Close(); sr.Close(); break; } catch (WebException e) { e.StackTrace.ToString(); System.Diagnostics.Trace.WriteLine(e.Message); if (true) continue; } } return fullhtml; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。