用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;
}

上一篇:如何为我们的程序编写开发文档——Java文档注释


下一篇:ER图