关键代码如下:
代码如下:
/// <summary>
/// 获取网页HTML源码
/// </summary>
/// <param name=”url”>链接 eg: </param>
/// <param name=”charset”>编码 eg:Encoding.UTF8</param>
/// <returns>HTML源码</returns>
public static string GetHtmlSource(string url, Encoding charset)
{
string _html = string.Empty;
try
{
HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();
using (Stream _stream = _response.GetResponseStream())
{
using (StreamReader _reader = new StreamReader(_stream, charset))
{
_html = _reader.ReadToEnd();
}
}
}
catch (WebException ex)
{
using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream()))
{
_html = sr.ReadToEnd();
}
}
catch (Exception ex)
{
_html = ex.Message;
}
return _html;
}
除声明外,跑步客文章均为原创,转载请以链接形式标明本文地点
本文地点:
相关内容
一个状态机的实现
SuperSocket入门--Telnet处事器和客户端请求措置惩罚惩罚
,