C# 读取指定URL的内容
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 读取指定URL的内容 /// <summary> /// 读取指定URL的内容 /// </summary> /// <param name="URL">指定URL</param> /// <param name="Content">该URL包含的内容</param> /// <returns>读取URL的状态</returns> public static string ReadHttp(string URL, ref string Content) { return ReadHttp(URL, "GET", ref Content); } public static string ReadHttp(string URL, string method, ref string Content) { string status = "ERROR"; HttpWebRequest Webreq = (HttpWebRequest)WebRequest.Create(URL); Webreq.Method = method; HttpWebResponse Webresp = null; StreamReader strm = null; try { Webresp = (HttpWebResponse)Webreq.GetResponse(); status = Webresp.StatusCode.ToString(); Encoding encod; if (Webresp.CharacterSet.ToLower() == "utf-8") { encod = Encoding.GetEncoding("utf-8"); } else { encod = Encoding.GetEncoding("gb2312"); } strm = new StreamReader(Webresp.GetResponseStream(), encod); Content = strm.ReadToEnd(); } catch (Exception ex) { } finally { if (Webresp != null) Webresp.Close(); if (strm != null) strm.Close(); } return (status); } #endregion