如何在ASP.NET中加载其他网页

时间:2011-01-30 08:23:37

标签: c# asp.net

我想从其他网站加载一些固定的部分到我的网络应用程序。 怎么做到这一点? 感谢。

2 个答案:

答案 0 :(得分:6)

你可以通过以下几种方式实现:

  1. 在客户端,将内容加载到<iframe>
  2. 在客户端,使用ajax加载内容并将其写入页面。
  3. 在服务器端,使用WebClient DownloadString加载页面并将其写入您的页面。
  4. <强>更新

    获得字符串后,您可以使用Html Agility Pack解析它并获取所需内容。 (也可在Nuget上找到)

答案 1 :(得分:5)

您可以使用WebRequest执行此任务:

string url = "http://somesite.com/somepage.php";
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    string contents = reader.ReadToEnd();
    //parse contents as you wish.......
    reader.Close();
}
response.Close();