ASP.NET使用HttpWebRequest讀取遠程網(wǎng)頁源代碼
更新時間:2016年03月26日 15:38:17 作者:haishu
本文分享了一個使用HttpWebRequest讀取遠程網(wǎng)頁的案例,供大家參考學(xué)習(xí)。
讀取遠程網(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?Web?API中實現(xiàn)監(jiān)控的方法
本文介紹了在ASP.NETCoreWebAPI中實現(xiàn)監(jiān)控的幾種流行開源工具,可以監(jiān)控API的性能、請求、響應(yīng)時間、錯誤率等,具有一定的參考價值,感興趣的可以了解一下2025-01-01
在ASP.NET中,設(shè)置Session的過期時間的方法
在ASP.NET中,設(shè)置Session的過期時間的方法,需要的朋友可以參考下2012-12-12
asp.net繼承IHttpHandler接口實現(xiàn)給網(wǎng)站圖片添加水印功能實例
這篇文章主要介紹了asp.net繼承IHttpHandler接口實現(xiàn)給網(wǎng)站圖片添加水印功能,實例分析了asp.net基于IHttpHandler接口實現(xiàn)網(wǎng)站圖片水印功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07

