Last active
August 16, 2020 09:57
-
-
Save greatcodeeer/4927fd1d12c654261a70 to your computer and use it in GitHub Desktop.
C# 获取网页源码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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