获取远程网页的内容之一(downmoon原创)
获取远程网页的内容之一(downmoon原创)
一、本机直接上网时:
1 #region 获取指定远程网页内容 2 /// <summary> 3 /// 获取指定远程网页内容 4 /// </summary> 5 /// <param name="strUrl">所要查找的远程网页地址</param> 6 /// <param name="timeout">超时时长设置,一般设置为8000</param> 7 /// <param name="enterType">是否输出换行符,0不输出,1输出文本框换行</param> 8 /// <param name="EnCodeType">编码方式</param> 9 /// <returns></returns> 10 /// 也可考虑 static string 11 12 public static string GetRequestString(string strUrl, int timeout, int enterType, Encoding EnCodeType) 13 { 14 string strResult; 15 try 16 { 17 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl); 18 myReq.Timeout = timeout; 19 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); 20 Stream myStream = HttpWResp.GetResponseStream(); 21 StreamReader sr = new StreamReader(myStream, EnCodeType); 22 StringBuilder strBuilder = new StringBuilder(); 23 24 while (-1 != sr.Peek()) 25 { 26 strBuilder.Append(sr.ReadLine()); 27 if (enterType == 1) 28 { 29 strBuilder.Append(" "); 30 } 31 } 32 strResult = strBuilder.ToString(); 33 } 34 catch (Exception err) 35 { 36 strResult = "请求错误:" + err.Message; 37 } 38 return strResult; 39 } 40 41 #endregion
二:通过域环境代理上网时这样就不行了! 下篇文章分解,呵呵!
获取远程网页的内容之二(downmoon原创)
在webForm中WebRequest\WebClient\WebBrowser获取远程页面源码的三种方式(downmoon)