locked
Using MSHTML to use / crawl a site RRS feed

  • Question

  • Hello...

    I need help with a problem i have.. im using MSHTML on my website to access a page on a site that requires login.. when i call a page on the site, im redirected to the login page.. what i need to do is get this login page , fill the logins, and get in to get to the page i need... i found the folllowing piece of code on the internet and it works fine when putting text in textbox, but it does not fire the click event of the button... i dunno y... i even tried IHTMLElementClick() but in vain :(

    can anyone pls help with this..?? 

    P.S : im not using the webbrowser control becuase this is a web application (website project to be precise) and this has to be invisible to the user.. the user will enter a url and press go.. 




    public enum HRESULT :uint { S_OK = 0, S_FALSE = 1, E_NOTIMPL = 0x80004001, E_INVALIDARG = 0x80070057, E_NOINTERFACE = 0x80004002, E_FAIL = 0x80004005, E_UNEXPECTED = 0x8000ffff } [ComVisible(true), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] public interface IPersistStreamInit : IPersist { new void GetClassID(ref Guid pClassID); [PreserveSig()] int IsDirty(); [PreserveSig()] HRESULT Load(UCOMIStream pstm); [PreserveSig()] HRESULT Save(UCOMIStream pstm, [MarshalAs(UnmanagedType.Bool)] bool fClearDirty); [PreserveSig()] HRESULT GetSizeMax([InAttribute(), Out(), MarshalAs(UnmanagedType.U8)] ref long pcbSize); [PreserveSig()] HRESULT InitNew(); } [ComVisible(true), ComImport(), Guid("0000010c-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] public interface IPersist { void GetClassID(ref Guid pClassID); } protected void Page_Load(object sender, EventArgs e) { string url = "http://images.google.com/"; mshtml.HTMLDocument objMSHTML = new mshtml.HTMLDocument(); mshtml.IHTMLDocument2 objMSHTML2; mshtml.IHTMLDocument3 objMSHTML3; int x = 10; //a dummy variable IPersistStreamInit objIPS; //here is the whole trick objIPS = (IPersistStreamInit)objMSHTML; objIPS.InitNew(); //you have to do it, if not you will always have readyState as "loading" objMSHTML2 = objMSHTML.createDocumentFromUrl(url, null); while (!(objMSHTML2.readyState == "complete")) { x = x + 1; } objMSHTML3 = (mshtml.IHTMLDocument3)objMSHTML2; IHTMLElementCollection d = objMSHTML3.getElementsByName("q"); IHTMLElementCollection de = objMSHTML3.getElementsByName("btnG"); HTMLInputElementClass cn = (HTMLInputElementClass)d.item("q", 0); cn.value = "biso"; HTMLInputElementClass bt = (HTMLInputElementClass)de.item("btnG", 0); bt.click(); // this does not fire string s = objMSHTML3. documentElement.innerHTML; // if u check (string s) in the html visualizer, ull get the google page with biso written in the text box }

     Much appreciated..


    Cheers,
    • Moved by Harry Zhu Wednesday, March 3, 2010 5:10 AM (From:Visual C# General)
    Saturday, February 27, 2010 7:23 PM

All replies

  • I used to have an app that needed to do something similar. here is the code that I used

               //you need to setup a class level bool value so that you don't fill in any fields until the page has finished loading.   
                public bool bBusy = false;   
      
                //then handle the document completed event to release code and allow it to continue   
             private void wb1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)   
            {   
                bBusy = false;   
            }   
      
      
      
                //the following code would be placed in the method that performs the action  
                bBusy = true;  
                wb1.Navigate(@"http://www.somesite.com/login.aspx");   
                 
                while (bBusy == true)   
                {   
                    Application.DoEvents();   
                }   
                foreach (HtmlElement htmle in wb1.Document.GetElementsByTagName("Input"))   
                {   
                      
                    switch (htmle.Name)   
                    {   
                        case "txtId": htmle.InnerText = "UserName"; break;   
                        case "txtPassword": htmle.InnerText = "Password"; break;   
                        case "btnLogin": bBusy = true;htmle.InvokeMember("click"); break;   
                    }   
                }   
                   
                while (bBusy == true)   
                {   
                    Application.DoEvents();   
                } 
    
    Saturday, February 27, 2010 11:12 PM
  • this is a windows app and ur using the webbrowser control, right???

    if so, this wont work cuz im using asp.ner and i cant/(and dont need) to use the webbrowser control...  
    Cheers,
    Sunday, February 28, 2010 6:03 AM
  • Yeah, this is from a forms app with a webbrowser control. sorry. not sure if this is possible. you might have better luck on an asp.net forum
    Sunday, February 28, 2010 6:19 AM
  • Hi,

    For the
    questions relating to asp.net ,please post to: http://forums.asp.net/.

    Thanks,
    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, March 3, 2010 5:10 AM