WebBrowser控件跨域访问页面内容

原文出处 :http://blog.csdn.net/nocky/article/details/6056802

源码出处:http://www.codecentrix.com/blog/wnd2doc_csharp/GetDocumentFromWindowCsharp.zip

原作者的博客:http://codecentrix.blogspot.com/

整理发现最后还是IE策略给限制了跨域访问,之前使用编程的办法和植入Script代码的办法,都是拒绝访问的。

不过现在用了下边的方法就可以解决了,代码也不多。Dll和代码均在工程文件里

 

//注意,需要手动引用 Interop.SHDocVw.dll,和.net 自带的Microsoft.mshtml

 

下边是实例:

using mshtml;

 

//需要调用的Frame

IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.Frames["frame_some"].DomWindow;
//获得Iframe doc
IHTMLDocument2 doc = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(win);
//body的信息
MessageBox.Show(doc.body.innerHTML);

//填充input
mshtml.IHTMLElementCollection inputs;
inputs = (mshtml.IHTMLElementCollection)doc.all.tags("INPUT");
mshtml.IHTMLElement element = (mshtml.IHTMLElement)inputs.item("txt_msg", 0);
mshtml.IHTMLInputElement inputElement = (mshtml.IHTMLInputElement)element;
inputElement.value = "填充信息";

posted @ 2016-04-16 17:50  龙骑科技  阅读(844)  评论(0编辑  收藏  举报