尝试通过C#程序登录网站 [英] Trying to log in to a website through a C# program

查看:90
本文介绍了尝试通过C#程序登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,所以我在其他问题中寻找了这个主题,但这些问题并不适合我.我正在尝试做的是我目前正在尝试使用c#程序(我正在尝试实现)登录学校的服务器.我想做的是知道页面的代码,所以我使用c#的网络浏览器进行导航,然后我只想在输入框中输入名称和密码,这就是我要坚持的地方.你能给我任何建议吗?

I'm new to C# so I looked for this topic in other questions but they weren't for me. What I am trying to do is I currently try to login to my school's servers using a c# program(Which I'm trying to implement). What I'm trying to do is I know the code of the page, so I am using web browser of c# to navigate then I just want to write name and password to the input boxes and this is where I stuck. Can you please give me any advices?

如果要查看页面: https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin

感谢您的建议.

这是我如何使用代码(添加了事件处理程序,但这是我第一次使用它,因此它提示我对象引用未设置为对象的实例"):

Here how I used the code( Added eventhandler but this is my first time using so it promts me "object reference not set to a instance of an object"):

            private void buttonGo_Click(object sender, EventArgs e)
    {
        try
        {
            string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";


            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);

            webBrowser1.Navigate(input);
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement userName = doc.GetElementById("UserID");
            HtmlElement pass = doc.GetElementById("PIN");
            HtmlElement submit = doc.GetElementById("Login");

            userName.SetAttribute("value", textID.Text);
            pass.SetAttribute("value", textPASS.Text);

            submit.InvokeMember("Click");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var webBrowser = sender as WebBrowser;
        webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
        MessageBox.Show(webBrowser.Url.ToString());
    } 
}

}

最后,我解决了我作弊的问题,但是设法解决了.这是工作代码:

Finally I solved problem I cheated a little but managed to solve. Here is the working code:

private void buttonGo_Click(object sender, EventArgs e)
        {
            try
            {
                string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";


                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);

                webBrowser1.Navigate(input);

                HtmlDocument doc = webBrowser1.Document;
                //HtmlElement userName = doc.GetElementById("UserID"); These not worked because ID of the elements were hidden so they are here to show which of these did not work.
                //HtmlElement pass = doc.GetElementById("password");
                HtmlElement submit = webBrowser1.Document.Forms[0].Document.All["PIN"].Parent.Parent.Parent.NextSibling.FirstChild;

                //userName.SetAttribute("value", textID.Text);
                //pass.SetAttribute("value", textPASS.Text);




                webBrowser1.Document.Forms[0].All["UserID"].SetAttribute("value", textID.Text);
                webBrowser1.Document.Forms[0].All["PIN"].FirstChild.SetAttribute("value", textPASS.Text);
                submit.InvokeMember("Click");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var webBrowser = sender as WebBrowser;
            webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
            MessageBox.Show(webBrowser.Url.ToString());
        } 

推荐答案

您需要首先找到用户名和密码字段的输入框,作为ID或节点.然后按如下方式分配它们:

You need to find the input boxes of the username and password fields as ID's or nodes first. Then assign them as such:

HtmlDocument doc = webBrowser1.Document;
HtmlElement email = doc.GetElementById("email");
HtmlElement pass = doc.GetElementById("pass");
HtmlElement submit = doc.GetElementById("LoginButton");

email.SetAttribute("value", "InsertYourEmailHere");
//Same for password

submit.InvokeMember("Click");

这篇关于尝试通过C#程序登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆