HTTP协议获取网页内容:
1 protected string GetPageInfo(string url, bool needHeard, string referer) 2 { 3 StringBuilder sb = new StringBuilder(); 4 HttpWebRequest request; 5 try 6 { 7 request = (HttpWebRequest)(WebRequest.Create(url)); 8 if (needHeard) 9 { 10 request.Method = "GET"; 11 request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US"); 12 request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); 13 request.Referer = referer; 14 request.Accept = "application/javascript, */*;q=0.8"; 15 request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; 16 } 17 } 18 catch (Exception e) 19 { 20 return e.Message; 21 } 22 request.KeepAlive = false; 23 HttpWebResponse response; 24 try 25 { 26 response = (HttpWebResponse)request.GetResponse(); 27 System.Text.Encoding encoding = System.Text.Encoding.Default; 28 Stream streamResponse = response.GetResponseStream(); 29 StreamReader streamReader = new StreamReader(streamResponse, encoding); 30 Char[] readBuff = new Char[256]; 31 int count = streamReader.Read(readBuff, 0, 256); 32 while (count > 0) 33 { 34 String outputData = new String(readBuff, 0, count); 35 sb.Append(outputData); 36 count = streamReader.Read(readBuff, 0, 256); 37 } 38 response.Close(); 39 return sb.ToString(); 40 } 41 catch (Exception ex) 42 { 43 return ex.Message; 44 } 45 }