ASP.NET读取远程网页源码 ASP.NET使用HttpWebRequest读取远程网页源代码
haishu 人气:0想了解ASP.NET使用HttpWebRequest读取远程网页源代码的相关内容吗,haishu在本文为您仔细讲解ASP.NET读取远程网页源码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:HttpWebRequest,网页,源码,下面大家一起来学习吧。
读取远程网页能做什么就不用多说了吧,做小偷程序或是采集,也就诸如此类了吧。
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; }
加载全部内容