【问题标题】:Getting SourceCode of Website获取网站源代码
【发布时间】:2017-12-24 05:15:01
【问题描述】:

我使用以下代码从 SharePoint 2010 网站获取 SourceCode:

try {
    WebRequest req = HttpWebRequest.Create("myLink");
    req.Method = "GET";
    req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    string source = "";
    using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream())) {
        source += reader.ReadToEnd();
    }
   }

从源字符串中,我能够搜索到我在网站上寻找的关键字。

现在 SharePoint 已迁移到 2016,我无法再查看源代码中的特定内容。

但是,可以使用例如 chrome 的集成 Web 开发工具来查看站点的结构。在这种情况下,我正在寻找的内容也是可见的。

如何使用 C# 以编程方式获取这些信息?

【问题讨论】:

  • 您可以尝试将您的请求与使用 Fiddler 的良好请求进行比较吗?注意:有些网站需要 UserAgent 才能给您所需的响应,其他标题可能是必需的。

标签: c# httpwebrequest webrequest html-content-extraction


【解决方案1】:

试试这个:

    using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
    {
        client .Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
        string htmlCode = client.DownloadString("myLink");
        //...
    }

【讨论】:

  • 不幸的是同样的结果。显示源代码但不是“真实”内容
  • 仍然无法正常工作。我得到了大约 400 行源代码,但缺少纯内容
猜你喜欢
  • 1970-01-01
  • 2012-11-28
  • 2023-02-14
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
  • 2019-01-14
  • 1970-01-01
相关资源
最近更新 更多