ASP.NET使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁(yè)源代碼
更新時(shí)間:2016年03月26日 15:38:17 作者:haishu
本文分享了一個(gè)使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁(yè)的案例,供大家參考學(xué)習(xí)。
讀取遠(yuǎn)程網(wǎng)頁(yè)能做什么就不用多說(shuō)了吧,做小偷程序或是采集,也就諸如此類(lèi)了吧。
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中獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之一(downmoon原創(chuàng))
- asp.net下獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))
- asp.net 網(wǎng)頁(yè)編碼自動(dòng)識(shí)別代碼
- asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁(yè)編碼
- asp.net(c#)做一個(gè)網(wǎng)頁(yè)數(shù)據(jù)采集工具
- HttpWebRequest和HttpWebResponse用法小結(jié)
- ASP.NET MVC中解析淘寶網(wǎng)頁(yè)出現(xiàn)亂碼問(wèn)題的解決方法
- asp.net 抓取網(wǎng)頁(yè)源碼三種實(shí)現(xiàn)方法
- C#中HttpWebRequest的用法詳解
- ASP.NET抓取網(wǎng)頁(yè)內(nèi)容的實(shí)現(xiàn)方法
相關(guān)文章
asp.net高效替換大容量字符實(shí)現(xiàn)代碼
每次替換完后,在下次替換時(shí)先排除這次替換的內(nèi)容,累加本次替換的內(nèi)容。2008-08-08
ASP.NET?Core?Web?API中實(shí)現(xiàn)監(jiān)控的方法
本文介紹了在ASP.NETCoreWebAPI中實(shí)現(xiàn)監(jiān)控的幾種流行開(kāi)源工具,可以監(jiān)控API的性能、請(qǐng)求、響應(yīng)時(shí)間、錯(cuò)誤率等,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
在ASP.NET中,設(shè)置Session的過(guò)期時(shí)間的方法
在ASP.NET中,設(shè)置Session的過(guò)期時(shí)間的方法,需要的朋友可以參考下2012-12-12
asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能實(shí)例
這篇文章主要介紹了asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能,實(shí)例分析了asp.net基于IHttpHandler接口實(shí)現(xiàn)網(wǎng)站圖片水印功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07

