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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步