日期:2014-05-18 浏览次数:21625 次
private string GetHTML(string url, string cookie) { if (String.IsNullOrEmpty(url)) { return null; } else { try { url = url.Replace(" ", "%20"); url = url.Replace("&", "&"); } catch { } } string html = null; string curCookie = null; HttpWebRequest myRequest = null; HttpWebResponse myResp = null; StreamReader myReader = null; for (int i = 0; i < 3; i++) { try { myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.AllowAutoRedirect = false; myRequest.Timeout = 60 * 1000; myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; myRequest.Headers.Add("Cookie", cookie); myRequest.KeepAlive = true; myResp = (HttpWebResponse)myRequest.GetResponse(); curCookie = myResp.Headers["Set-Cookie"]; curCookie = curCookie.Substring(0, curCookie.IndexOf(";")); myReader = new StreamReader(myResp.GetResponseStream(), Encoding.Default, true); html = myReader.ReadToEnd(); } catch (Exception e) { // Console.WriteLine(e.ToString()); continue; } finally { if (myReader != null) { myReader.Close(); } if (myResp != null) { myResp.Close(); } } if (html == null) { continue; } else if (html.IndexOf("Object moved") > 0) //手动获取转向地址,再次获取html代码 { int pos1 = html.IndexOf("http://"); int pos2 = html.IndexOf("\">"); url = html.Substring(pos1, pos2 - pos1); html = GetHTML(url, curCookie); } break; } return html; }