c#网站通过WebBrowser Control登录并导航到另一个页面

时间:2017-11-29 13:31:59

标签: c# html winforms httprequest webbrowser-control

我想使用WinformsWebBrowser控件登录网页。登录后我想导航到另一个页面。我可以成功登录,但之后我不知道如何导航到所需的页面。我登录后获得的页面有一个下拉菜单,我想导航到该下拉列表中的一个项目。

该元素的页面检查给我这样的东西:

<a href="/tn/Dispatcher?reqkey=HTCRTIl0XDzcKP&amp;TARGET=file_upload">
    Text</a>
每次都会自动生成

reqkey。我不能简单地说WebBrowser.Navigate(URL)因为我收到错误。而且我也不知道如何获得reqkey以及如何通过它。

这是我的登录代码:

  class Caller {
    private string _URL;
    private string _id;
    private string _nid;
    private string _pin;

    public Caller() {
    }

    public void SetParams(string URL, string id, string nid, string pin) {
        this._URL = URL;
        this._tid = id;
        this._benid = nid;
        this._pin = pin;

    }

    public void CreateBrowser() {
        try {
            Form wbForm = new Form();
            wbForm.Text = "Form 1";
            WebBrowser wbControl = new WebBrowser();
            int height = Screen.PrimaryScreen.Bounds.Height;
            int width = Screen.PrimaryScreen.Bounds.Width;
            wbControl.Height = height;
            wbControl.Width = width;
            wbForm.WindowState = FormWindowState.Maximized;
            wbForm.Controls.Add(wbControl);
            wbControl.DocumentCompleted += WebBrowserDocumentCompleted;
            wbControl.Navigate(_URL);

            wbForm.Show();

        } 
        catch (Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }

    public void InputLoginData(WebBrowser wb) {
        try {
            var inputElements = wb.Document.GetElementsByTagName("input");
            foreach (HtmlElement i in inputElements) {

                if (i.GetAttribute("id").Equals("id")) {
                    i.InnerText = _tid;
                }

                if (i.GetAttribute("id").Equals("nid")) {
                    i.Focus();
                    i.InnerText = _nid;
                }

                if (i.GetAttribute("id").Equals("pin")) {
                    i.Focus();
                    i.InnerText = _pin;
                }
                if (i.GetAttribute("value").Equals("Login")) {
                    i.InvokeMember("click");
                }

            }


         } 

        catch (Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }

    public void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if ((sender as WebBrowser).ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete) {
            InputLoginData(sender as WebBrowser);
        }
    }


}

感谢任何帮助。感谢

0 个答案:

没有答案