【问题标题】:Getting source of web page from open window in C#从 C# 中打开的窗口获取网页的源代码
【发布时间】:2014-01-30 09:49:18
【问题描述】:

我想用c#从任何chrome、firefox、ie等窗口获取网页源代码 我应该用什么?

我的应用:

【问题讨论】:

    标签: c# html window webpage


    【解决方案1】:

    你应该使用这个代码:

    using System.Net;
    //...
    using (WebClient webClient = new WebClient ())
    {
        client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html");
    
        // Or you can get the file content without saving it:
        string htmlCode = client.DownloadString("http://yoursite.com/page.html");
        //...
    }
    

    在 htmlCode 中,您将按预期获得 html 源代码并将其显示在文本查看器上。

    【讨论】:

    • client.DownloadFile("yoursite.com/page.html", @"C:\localfile.html");但 url 应该是动态的。如何从打开的浏览器窗口获取 url?
    • 您可以通过 ajax 或对服务器的 post 调用发送它(假设使用 Jquery 或任何其他适当的 javascript 库)。您将在 javascript 中通过 window.location.href 找到当前 URL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2011-11-30
    相关资源
    最近更新 更多