ASP.NET使用HttpWebRequest讀取遠程網(wǎng)頁源代碼
更新時間:2016年03月26日 15:38:17 作者:haishu
本文分享了一個使用HttpWebRequest讀取遠程網(wǎng)頁的案例,供大家參考學習。
讀取遠程網(wǎng)頁能做什么就不用多說了吧,做小偷程序或是采集,也就諸如此類了吧。
public string GetPage(string url)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string html = reader.ReadToEnd();
return html;
}
}
catch
{
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (reader != null)
reader.Close();
if (request != null)
request = null;
}
return string.Empty;
}
您可能感興趣的文章:
- 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抓取網(wǎng)頁內(nèi)容的實現(xiàn)方法
相關(guān)文章
在?ASP.NET?Core?中使用?HTTP?標頭傳播詳情
這篇文章主要介紹了在?ASP.NET?Core?中使用?HTTP?標頭傳播詳情,文章通過,我們創(chuàng)建?ServerA、ServiceB?兩個?Web?API?項目展開內(nèi)容,需要的朋友可以參考一下2022-04-04
asp.net 數(shù)據(jù)庫連接類代碼(SQL)
asp.net數(shù)據(jù)庫連接類(SQL) 代碼,需要的朋友可以參考下。2010-03-03
用Fine Uploader+ASP.NET MVC實現(xiàn)ajax文件上傳[代碼示例]
Fine Uploader(http://fineuploader.com/)是一個實現(xiàn) ajax 上傳文件的 Javascript 組件2013-01-01
ASP.NET開發(fā)中經(jīng)常用到10款工具軟件介紹
從事.NET開發(fā)也好幾年了,工作過程中積累一些軟件工具,分享給大家,排名不分先后,希望對大家有所幫助。2016-04-04

