用asp.net c# HttpWebRequest获取网页源代码_asp.net教程-查字典教程网
用asp.net c# HttpWebRequest获取网页源代码
用asp.net c# HttpWebRequest获取网页源代码
发布时间:2015-06-05 来源:查字典编辑
摘要:该方法需要传递目标网页的编码方式,比如System.Text.Encoding.Default或者System.Text.Encoding....

该方法需要传递目标网页的编码方式,比如System.Text.Encoding.Default或者System.Text.Encoding.UTF8

如果哪位高手知道如何自动判断目标页面的编码格式,请在评论中告知。谢谢!

///

/// 获取源代码

///

///

///

///

public static string GetPage(string url, Encoding encoding)

{

HttpWebRequest request = null;

HttpWebResponse response = null;

StreamReader reader = null;

try

{

request = (HttpWebRequest)WebRequest.Create(url);

request.UserAgent = "";

request.Timeout = 20000;

request.AllowAutoRedirect = false;

response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)

{

reader = new StreamReader(response.GetResponseStream(), encoding);

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教程学习
    热门asp.net教程学习
    编程开发子分类