2016-11-28 55 views
-3

我需要从网站获取源代码,该网站采用框架结构。将HTML源代码保存为WinForms应用程序中的字符串

我已经有一个Windows窗体应用程序,其WebBrowser功能集成到它。
当我做一个右键单击并选择“查看源代码”时,它会打开一个新的文本文档,其中包含我需要的信息。

我已经试过webBrowser.Document,webBrowser.DocumentTextwebBrowser.DocumentStream,但所有这些只给我其他信息,我不需要。

该网站不是静态的(这是一个聊天),它不会做会话,因此我不能使用Webclient.DownloadFile
我需要持续连接到网站几个小时,而无需刷新网站。我没有看到在Windows Forms中使用webBrowser的方法。

根据要求,这是网站,我说的是:http://server2.webkicks.de/stackoverflow-test/
您可以通过在第三个文本框中填写一些用户名作为访客登录。

+1

发布□请之前做一些研究。 – Tatranskymedved

+3

可能的重复[如何在C#中下载HTML源代码](http://stackoverflow.com/questions/599275/how-can-i-download-html-source-in-c-sharp) – Tatranskymedved

+0

为什么不你只需使用'HttpClient'从wesbite下载? https://www.dotnetperls.com/httpclient –

回答

1

当你希望得到的动态HTML内容,并webBrowser.DocumentwebBrowser.DocumentTextwebBrowser.DocumentStream不工作,你的愿望。

这里的技巧:您可以随时从C#运行您的自定义JavaScript代码。这里是你如何能得到当前的HTML你WebBrowser控制:

webBrowser.Document.InvokeScript("eval", new string[]{"document.body.outerHTML"}); 

参考How to inject Javascript in WebBrowser control?

更新

对于iframedocument里面,你可以尝试以下方法:

webBrowser.Document.InvokeScript("eval", new string[]{"document.querySelector(\"iframe\").contentWindow.document.documentElement.outerHTML"}); 

另一个更新

当你的网站包含frame,而不是iframe,这里是你如何获得该的html内容:

webBrowser.Document.InvokeScript("eval", new string[]{"document.querySelector(\"frame[name='mainframe'\").contentWindow.document.documentElement.outerHTML"}); 

最终测试和更新工作

querySelectorWebControl工作。因此,解决方法是:为您的<frame>提供一些id,并使用该id获取该<frame>元素。这里是你如何实现你的任务。

HtmlElement frame = webBrowser1.Document.GetElementsByTagName("frame").Cast<HtmlElement>().FirstOrDefault(m => m.GetAttribute("name") == "mainframe"); 
if (frame != null) 
{ 
    frame.Id = "RandID_" + DateTime.Now.Ticks; 
    string html = webBrowser1.Document.InvokeScript("eval", new string[] { "document.getElementById('" + frame.Id + "').contentWindow.document.documentElement.outerHTML" }).ToString(); 
    Console.WriteLine(html); 
} 
else 
{ 
    MessageBox.Show("Frame not found"); 
} 
+0

感谢您的回答。虽然这确实给了我html源代码,但它不是我正在寻找的那个。我想我需要框架的源代码,我正在看。 虽然注入Javascript是最好的方法,正如你所建议 – NotTelling

+0

@TristanB。你的问题。在任何地方都不会说'Iframe'。不用担心,我正在更新iframe的答案。 – sam

+0

对不起。我无法将我的问题纳入技术术语,因为我不是专业人员,而是学习者。谢谢! – NotTelling

0

如果您的网站的目标使用SSL协议(HTTPS),您可以尝试添加用户代理是这样的:

using (WebClient myWebClient = new WebClient()) 
          { 
           myWebClient.Headers.Add("User-Agent: Other");    
           myWebClient.DownloadFile(new System.Uri("https://mywebsite.com//somefile"), "D:\\temp\\somefile"); 
          } 

如果您的网站的目标需要登录,然后您登录到您的websitetarget在Chrome和使用EditThisCookie扩展复制你的Cookie,并尝试这一个:

using (WebClient myWebClient = new WebClient()) 
          { 
           myWebClient.Headers.Add("User-Agent: Other"); 
           myWebClient.Headers.Add(HttpRequestHeader.Cookie, "mycookies copies from EditThisCookie"); 
           myWebClient.DownloadFile(new System.Uri("https://mywebsite.com//somefile"), "D:\\temp\\somefile"); 
          } 
+0

感谢您的回答。在我的情况下,我不需要下载一个文件,但保持不断跟踪快速变化的HTML。为了甚至到达那个html,我正在寻找,有问题的网站需要打开。如果我关闭它,我将需要重新登录。 您的答案适用于这些条件吗? – NotTelling

+0

对于您的问题的第一部分,您可以尝试DownloadString而不是DownloadFile,然后尝试在其中添加一段时间(true)和一个Thread.Sleep(2000),这意味着您将每隔2000ms检查一次目标页面的内容 –

+0

对于第二部分,cookie有一个到期日期,这意味着如果它过期了,您将无法再获取目标页面的内容,因此您得到的唯一解决方案就是手动完成,再次登录,复制您的cookies并将其插入到您的Web客户端页眉上。 –

相关问题