using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Web
{
/// <summary>
/// 公共方法类
/// </summary>
public class WebHandler
{
/// <summary>
/// 获取网页的HTML码
/// </summary>
/// <param name="url">链接地址</param>
/// <param name="encoding">编码类型</param>
/// <returns></returns>
public static string GetHtmlStr(string url, string encoding)
{
string htmlStr = "";
try
{
if (!String.IsNullOrEmpty(url))
{
WebRequest request = WebRequest.Create(url);//实例化WebRequest对象
WebResponse response = request.GetResponse();//创建WebResponse对象
Stream datastream = response.GetResponseStream(); //创建流对象
Encoding ec = Encoding.Default;
if (encoding == "UTF8")
{
ec = Encoding.UTF8;
}
else if (encoding == "Default")
{
ec = Encoding.Default;
}
StreamReader reader = new StreamReader(datastream, ec);
htmlStr = reader.ReadToEnd(); //读取网页内容
reader.Close();
datastream.Close();
response.Close();
}
}
catch { }
return htmlStr;
}
}
}
如果觉得《C#抓取网页HTML内容》对你有帮助,请点赞、收藏,并留下你的观点哦!