none
Webbrowser 登录成功后有时会莫名其妙回退到登录页 RRS feed

  • 问题

  • 我用.net 3.5 webbrower 做自动登录,登录后进入第二个页面。 可有时登录进入第二个页面后,莫名其妙又马上回到登录页面了。代码如下,求大神帮忙解决。

      public frmMain()
            {
                InitializeComponent();
          
              //  wb.Navigate(url, false);//在当面窗口打开 
                wb.DocumentCompleted+=wb_DocumentCompleted;
                wb.Navigated +=wb_Navigated;
                
                loginUrl = ConfigurationManager.AppSettings["LoginUrl"];
                menuUrl = ConfigurationManager.AppSettings["MenuUrl"];
                bookUrl = ConfigurationManager.AppSettings["BookUrl"];
            }
    
            private void wb_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
               
                IHTMLDocument2 vDocument = (IHTMLDocument2)wb.Document.DomDocument;
                //vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
                vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
               
            }
    
            private void btnBooking_Click(object sender, EventArgs e)
            {
                var btn = (Button)sender;
                btn.Enabled = false;
                StartBook();
                btn.Enabled = true;
            }
                
            private void StartBook()
            {          
    
                bookInfo = Context.DeserializeBookInfo();      
                wb.Navigate(loginUrl,false);
            }
    
           
            private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
              
    
               if (wb.ReadyState < WebBrowserReadyState.Complete ) return;        
            
    
               if(e.Url.ToString()==loginUrl)
                {
                    HtmlDocument doc = wb.Document;
                    Login(doc);
                }
                           
                     
    
                WriteLog(DateTime.Now.ToString()+"  Url: "+e.Url.ToString());
                WriteLog(DateTime.Now.ToString()+" LocalPath: "+e.Url.LocalPath);
                WriteLog(DateTime.Now.ToString()+" AbsolutePath: " + e.Url.AbsolutePath);
                WriteLog(DateTime.Now.ToString()+" AbsoluteUri: " + e.Url.AbsoluteUri);
                WriteLog("-----------------------------------");
                   
                
            }
    
            private void Login(HtmlDocument doc)
            {
    
    
                HtmlElement btnlogin = wb.Document.GetElementById("loginForm");
                HtmlElement tbUserid = wb.Document.All["j_username"];
                HtmlElement tbPasswd = wb.Document.All["j_password"];
                HtmlElement tbCaptcha = wb.Document.All["j_captcha"];
                HtmlElement tbImage = wb.Document.All["captchaImage"];
    
                if (tbUserid == null)
                {
                    //wb.Navigate(loginUrl, false);
                    return;
                }
    
                string result = this.Ocr(GetWebImage(wb, tbImage));
                //  label1.Text = result;
    
    
                tbUserid.SetAttribute("value", ConfigurationManager.AppSettings["UserId"]);
                tbPasswd.SetAttribute("value", ConfigurationManager.AppSettings["Password"]);
                tbCaptcha.SetAttribute("value", result);
                //执行正常流程代码…………
    
                btnlogin.InvokeMember("submit");
            }


    • 已编辑 Cuiqs 2014年9月2日 14:56
    2014年9月2日 14:55

全部回复

  • 你好:

    我不知道你的登陆页面的网址,没有办法测试你的代码,我建议你单步调试一下你的代码,在自动跳转回登陆页面的时候检查一下是否你的代码作了哪些操作,如果仅仅是有时候出现这种问题我认为有可能是网站本身的问题。你尝试一下在IE里面登陆是否会有这个问题出现。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月4日 1:41
    版主