- /// <summary>
- /// 网络数据抓取(默认编码格式为gb2312)
- /// </summary>
- /// <param name="url">访问地址</param>
- /// <returns>访问的返回结果</returns>
- public static string GetURL(string url)
- {
- HttpwebResponse res = null;
- string strResult = "";
- try
- {
- var req = (HttpWebRequest) WebRequest.Create(url);
- req.Method = "GET";
- req.ContentType = "application/x-www-form-urlencoded";
- req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
- var cookieCon = new CookieContainer();
- req.CookieContainer = cookieCon;
- res = (HttpWebResponse) req.GetResponse();
- Stream ReceiveStream = res.GetResponseStream();
- string encodeheader = res.ContentType;
- Encoding encode = Encoding.GetEncoding("GB2312");
- var sr = new StreamReader(ReceiveStream, encode);
- var read = new Char[256];
- int count = sr.Read(read, 0, 256);
- while (count > 0)
- {
- var str = new String(read, 0, count);
- strResult += str;
- count = sr.Read(read, 0, 256);
- }
- }
- finally
- {
- if (res != null)
- {
- res.Close();
- }
- }
- return strResult;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1108201513395.html
来源: http://www.codesnippet.cn/detail/1108201513395.html