关键代码:
/// <summary>/// 获取网页HTML源码/// </summary>/// <param name="url">链接 eg:http://www.baidu.com/ </param>/// <param name="charset">编码 eg:Encoding.UTF8</param>/// <returns>HTML源码</returns>publicstaticstringGetHtmlSource(stringurl, 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 =newStreamReader(_stream, charset))
{
_html = _reader.ReadToEnd();
}
}
}catch(WebException ex)
{using(StreamReader sr =newStreamReader(ex.Response.GetResponseStream()))
{
_html = sr.ReadToEnd();
}
}catch(Exception ex)
{
_html = ex.Message;
}return_html;
}
.csharpcode, .csharpcodePRe
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp{ background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }测试代码:
publicstaticvoidGetHtmlSourceTest()
{string_url ="http://www.baidu.com/";string_htmlSource = HttpWebRequestUtilsV2.GetHtmlSource(_url, Encoding.UTF8);
Console.WriteLine(_htmlSource);
}
测试效果:
希望有所帮助!
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }