用asp.net c# HttpWebRequest获取网页源代码

Posted 追梦青年(李海淑)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用asp.net c# 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;
}

以上是关于用asp.net c# HttpWebRequest获取网页源代码的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Razor——ASP.NET Razor - C#代码语法

用asp.net c# HttpWebRequest获取网页源代码

用两个列表框asp.net c#绑定gridview

C# ASP.NET中的Label怎么实现绝对定位?

asp.net中radiobutton 的问题 C#

用vs2010平台,c#写的asp.net网站,动态增加和减少Textbox