ASP.NET抓取網(wǎng)頁內(nèi)容的實現(xiàn)方法
本文實例講述了ASP.NET抓取網(wǎng)頁內(nèi)容的實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
一、ASP.NET 使用HttpWebRequest抓取網(wǎng)頁內(nèi)容
/// 用HttpWebRequest取得網(wǎng)頁源碼
/// 對于帶BOM的網(wǎng)頁很有效,不管是什么編碼都能正確識別
/// </summary>
/// <param name="url">網(wǎng)頁地址" </param>
/// <returns>返回網(wǎng)頁源文件</returns>
public static string GetHtmlSource2(string url)
{
//處理內(nèi)容
string html = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*"; //接受任意文件
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //
request.AllowAutoRedirect = true;//是否允許302
//request.CookieContainer = new CookieContainer();//cookie容器,
request.Referer = url; //當前頁面的引用
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.Default);
html = reader.ReadToEnd();
stream.Close();
return html;
}
二、ASP.NET 使用 WebResponse 抓取網(wǎng)頁內(nèi)容
{
string sException = null;
string sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;
try
{
oWebRps = oWebRqst.GetResponse();
}
catch (WebException e)
{
sException = e.Message.ToString();
}
catch (Exception e)
{
sException = e.ToString();
}
finally
{
if (oWebRps != null)
{
StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
- asp.net中獲取遠程網(wǎng)頁的內(nèi)容之一(downmoon原創(chuàng))
- asp.net下獲取遠程網(wǎng)頁的內(nèi)容之二(downmoon原創(chuàng))
- asp.net 網(wǎng)頁編碼自動識別代碼
- asp.net HttpWebRequest自動識別網(wǎng)頁編碼
- asp.net(c#)做一個網(wǎng)頁數(shù)據(jù)采集工具
- HttpWebRequest和HttpWebResponse用法小結(jié)
- ASP.NET MVC中解析淘寶網(wǎng)頁出現(xiàn)亂碼問題的解決方法
- asp.net 抓取網(wǎng)頁源碼三種實現(xiàn)方法
- C#中HttpWebRequest的用法詳解
- ASP.NET使用HttpWebRequest讀取遠程網(wǎng)頁源代碼
相關(guān)文章
asp.net IList查詢數(shù)據(jù)后格式化數(shù)據(jù)再綁定控件
這篇文章送給.net初學者或者遇到類似問題的朋友,就是IList如何格式化數(shù)據(jù)再綁定,我看到網(wǎng)上沒有多少朋友講到這方面的最基本的問題,現(xiàn)在我簡單說說吧,代碼我就截取其中一些講,如果不明白的朋友可以留言或者聯(lián)系我。2009-11-11用ASP.NET做的個性化的郵件發(fā)送系統(tǒng)
如果要你用ASP來做一個郵件發(fā)送系統(tǒng),你一定認為這是一個比較復雜的工作。其實也的確是這樣。但當他的后繼產(chǎn)品ASP.NET被推出以后,他的強大功能就使的這一切就變的相對簡單了。真的這樣神奇么?我們就通過ASP.NET做一個郵件發(fā)送系統(tǒng),看看到底有什么奧秘,是不是真的簡單。2008-02-02基于.NET的FluentValidation數(shù)據(jù)驗證實現(xiàn)
這篇文章主要介紹了基于.NET的FluentValidation數(shù)據(jù)驗證實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11剖析Asp.Net Web API路由系統(tǒng)---WebHost部署方式
這篇文章主要介紹了剖析Asp.Net Web API路由系統(tǒng)---WebHost部署方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02