c# - How to get the new window's URL with WebBrowser -


i want crawl webpage. problem is, webpage has encrypted link can clicked. using webbrowser.navigate wont work. managed simulate clicking action , opened new window. want new window's url.

private void form1_load(object sender, eventargs e)     {         webbrowser1.navigate(@"http://www.downarchive.ws/software/downloaders/795011-easy-mp3-downloader-4536.html");     }      private void webbrowser1_navigated(object sender, webbrowsernavigatedeventargs e)     {         htmlelementcollection links = webbrowser1.document.getelementsbytagname("a");          foreach (htmlelement link in links)         {             if (link.getattribute("href").contains(@"http://www.downarchive.ws/engine/go.php?url="))             {                 link.invokemember("click");                 break;             }         }     }      private void webbrowser1_newwindow(object sender, canceleventargs e)     {         var webbrowser = (webbrowser)sender;         messagebox.show(webbrowser.url.tostring());      } 

1) don't need invoke click. better use navigate method. when invoke click, link may open in new window, additional javascript may executed, etc.

2) if need url after redirections, there documentcompleted event:

webbrowserdocumentcompletedeventhandler ondocumentcompleted = (sender, e) => {     uri theulrthatyouneed = e.url;     webbrowser1.documentcompleted -= ondocumentcompleted; }; webbrowser1.documentcompleted += ondocumentcompleted; webbrowser1.navigate("your encrypted url"); 

3) if link open in external ie window, it's gone - can't control external browser , receive events it. sometimes, redirections can open new window. prevent this, can use extended webbrowser class:

namespace extendedwebbrowser { [comimport, typelibtype(typelibtypeflags.fhidden), interfacetype(cominterfacetype.interfaceisidispatch), guid("34a715a0-6587-11d0-924a-0020afc7ac4d")] public interface dwebbrowserevents2 {     /// <summary>     ///      /// </summary>     /// <param name="ppdisp">     /// interface pointer that, optionally, receives idispatch interface     /// pointer of new webbrowser object or internetexplorer object.     /// </param>     /// <param name="cancel">     /// value determines whether current navigation should canceled     /// </param>     /// <param name="dwflags">     /// flags nwmf enumeration pertain new window     /// see http://msdn.microsoft.com/en-us/library/bb762518(vs.85).aspx.     /// </param>     /// <param name="bstrurlcontext">     /// url of page opening new window.     /// </param>     /// <param name="bstrurl">the url opened in new window.</param>     [dispid(0x111)]     void newwindow3(         [in, out, marshalas(unmanagedtype.idispatch)] ref object ppdisp,         [in, out] ref bool cancel,         [in] uint dwflags,         [in, marshalas(unmanagedtype.bstr)] string bstrurlcontext,         [in, marshalas(unmanagedtype.bstr)] string bstrurl); } public partial class webbrowserex : webbrowser {     axhost.connectionpointcookie cookie;     dwebbrowserevent2helper helper;     [browsable(true)]     public event eventhandler<webbrowsernewwindoweventargs> newwindow3;     [permissionsetattribute(securityaction.linkdemand, name = "fulltrust")]     public webbrowserex() {     }     /// <summary>     /// associates underlying activex control client can      /// handle control events including newwindow3 event.     /// </summary>     [permissionsetattribute(securityaction.linkdemand, name = "fulltrust")]     protected override void createsink() {         base.createsink();          helper = new dwebbrowserevent2helper(this);         cookie = new axhost.connectionpointcookie(             this.activexinstance, helper, typeof(dwebbrowserevents2));     }     /// <summary>     /// releases event-handling client attached in createsink method     /// underlying activex control     /// </summary>     [permissionsetattribute(securityaction.linkdemand, name = "fulltrust")]     protected override void detachsink() {         if (cookie != null) {             cookie.disconnect();             cookie = null;         }         base.detachsink();     }     /// <summary>     ///  raises newwindow3 event.     /// </summary>     protected virtual void onnewwindow3(webbrowsernewwindoweventargs e) {         if (this.newwindow3 != null) {             this.newwindow3(this, e);         }     }     private class dwebbrowserevent2helper : standardolemarshalobject, dwebbrowserevents2 {         private webbrowserex parent;         public dwebbrowserevent2helper(webbrowserex parent) {             this.parent = parent;         }         /// <summary>         /// raise newwindow3 event.         /// if instance of webbrowser2eventhelper associated underlying         /// activex control, method called when newwindow3 event         /// fired in activex control.         /// </summary>         public void newwindow3(ref object ppdisp, ref bool cancel, uint dwflags,             string bstrurlcontext, string bstrurl) {             var e = new webbrowsernewwindoweventargs(bstrurl, cancel);             this.parent.onnewwindow3(e);             cancel = e.cancel;         }     } } public class webbrowsernewwindoweventargs : eventargs {     public string url { get; set; }     public boolean cancel { get; set; }     public webbrowsernewwindoweventargs(string url, boolean cancel) {         this.url = url;         this.cancel = cancel;     } } }  void webbrowser_newwindow(object sender, webbrowsernewwindoweventargs e) {   if (!string.isnullorempty(e.url)) {      //prevent new window     e.cancel = true;      // navigate url new window     navigate(e.url);   } } 

so, if replace webbrowser control on improved version, able prevent new window.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -