Skip to content

Instantly share code, notes, and snippets.

@greatcodeeer
Last active August 16, 2020 09:57
Show Gist options
  • Save greatcodeeer/4927fd1d12c654261a70 to your computer and use it in GitHub Desktop.
Save greatcodeeer/4927fd1d12c654261a70 to your computer and use it in GitHub Desktop.
C# 获取网页源码
/// <summary>
/// 获取网页源码
/// </summary>
/// <param name="Url">链接</param>
/// <param name="CharSet">编码</param>
/// <returns></returns>
public static string GetSource(string Url, string CharSet = "GB2312")
{
using (WebClient myWebClient = new WebClient())
{
using (Stream myStream = myWebClient.OpenRead(Url))
{
using (StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding(CharSet)))
{
return sr.ReadToEnd();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment