c#

位置:IT落伍者 >> c# >> 浏览文章

C# 获取网页html源文件


发布日期:2022年04月02日
 
C# 获取网页html源文件

/// <summary>
/// 获取网页html源文件
/// </summary>
/// <param name="url">网页地址</param>
/// <param name="encodingStr">网页文件编码字符串</param>
/// <returns>html源文件</returns>
#region GetPageSource
public static string GetPageSource(string url string encodingStr)
{
HttpWebResponse res = null;
string strResult = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequestCreate(url);
//reqMethod = "POST";
reqKeepAlive = true;
reqContentType = "application/xwwwformurlencoded";
reqAccept = "text/Htmlapplication/xhtml+XMLapplication/xml;q=*/*;q=";
reqUserAgent = "Mozilla/ (Windows; U; Windows NT ; zhCN; rv:) Gecko/ Firefox/";
res = (HttpWebResponse)reqGetResponse();
StreamReader reader = new StreamReader(resGetResponseStream() EncodingGetEncoding(encodingStr));
strResult = readerReadToEnd();
readerClose();
}
catch
{

}
finally
{
if (res != null)
{
resClose();
}
}
return strResult;
}

上一篇:.NET单点登陆的实现方法及思路

下一篇:C#高级编程:给数据库添加会议数据[2]