VB.NET Login/Submit button ID/Name

08/08/2014 23:27 Cheating-nl#1
Hi,

I trying to login using a webbrowser in vb.net with the following:

Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        WebBrowser1.Document.GetElementById("username").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)

WebBrowser1.Document.GetElementById("ID_HERE").InvokeMember("click")


    End Sub
However, I can't find the ID/Name of the Submit/Login button.

Website: [Only registered and activated users can see links. Click Here To Register...]

Somebody able to help me out?

Thanks in advance
08/09/2014 00:13 Terreox#2
Quote:
Originally Posted by Cheating-nl View Post
However, I can't find the ID/Name of the Submit/Login button.

Website: [Only registered and activated users can see links. Click Here To Register...]

Somebody able to help me out?

Thanks in advance
That's because the submit button has no id assigned to it.
I don't know if you can use XPath with your Browser control but if you can then just learn some basics about XPath and get the submit button with its XPath.
If you can't use XPath then you could try to execute some piece of Javascript to get and click the submit button.
08/09/2014 12:32 Cheating-nl#3
Hmm okey, I think I just gonna try it with webrequests then.
Should be a better alternative too.
08/09/2014 15:28 PC Jones#4
If the button has a unique class-name you can do this:
Code:
            For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
                If element.GetAttribute("className") = "THE_CLASS_NAME" Then
                    element.invokeMember("click")
                End If
            Next
But of course, WebRequests are much better.

edit: The className seems to be bgcdw_button bgcdw_login_form_login
I've tried it and it worked
08/09/2014 16:59 Terreox#5
Quote:
Originally Posted by PC Jones View Post
If the button has a unique class-name you can do this:
Code:
            For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
                If element.GetAttribute("className") = "THE_CLASS_NAME" Then
                    element.invokeMember("click")
                End If
            Next
But of course, WebRequests are much better.

edit: The className seems to be bgcdw_button bgcdw_login_form_login
I've tried it and it worked
You could do this but a class is not unique by nature :) it might work in this case but I would generally not rely on this even if a class appears to be unique.
08/09/2014 17:54 ZiD.#6
Use webrequest for logins etc...


They are better & faster.
08/10/2014 18:04 Cheating-nl#7
Quote:
Originally Posted by PC Jones View Post
If the button has a unique class-name you can do this:
Code:
            For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
                If element.GetAttribute("className") = "THE_CLASS_NAME" Then
                    element.invokeMember("click")
                End If
            Next
But of course, WebRequests are much better.

edit: The className seems to be bgcdw_button bgcdw_login_form_login
I've tried it and it worked
Thanks! I'll maybe gonna use this, but first I try to use webrequests for now.

EDIT: Working with the webrequests now, can't find the right referer for Seafight login.
Found this but doesn't seem to work:

Code:
Dim postData As String = "referer=http%253A%252F%252Fwww." & NsTextBox3.Text & ".seafight.bigpoint.com%252Fusername%253D" & NsTextBox1.Text & "%2526password%253D" & NsTextBox2.Text
NsTextbox1.Text contains the Username
NsTextbox2.Text contains the Password
NsTextbox3.Text contains the server shortcut (ex. int1)
08/11/2014 07:14 Mostey#8
What do you mean by "referer"? The HTTP header?

Did you perform a GET request before posting your data to the website? There might have been some cookies you've missed.
08/11/2014 13:17 Cheating-nl#9
Yes, I mean the HTTP header.

The code I have so far:

Code:
Dim logincookie As CookieContainer

    Private Sub NsButton1_Click(sender As Object, e As EventArgs) Handles NsButton1.Click

        Dim postData As String = "referer=http%253A%252F%252Fwww." & NsTextBox3.Text & ".seafight.bigpoint.com%252Fusername%253D" & NsTextBox1.Text & "%2526password%253D" & NsTextBox2.Text
        Dim tempCookies As New CookieContainer
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)

        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://" & NsTextBox3.Text & ".seafight.bigpoint.com/index.es?action=internalHome&loginDone=true"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = tempCookies
        postReq.ContentType = "application/x-www.form.urlencoded"
        postReq.Referer = "http://" & NsTextBox3.Text & ".seafight.bigpoint.com/index.es?action=internalHome&loginDone=true"
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
        postReq.ContentLength = byteData.Length

        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()
        Dim postresponse As HttpWebResponse

        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
        tempCookies.Add(postresponse.Cookies)
        logincookie = tempCookies
        Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

        Dim thepage As String = postreqreader.ReadToEnd

        RichTextBox1.Text = thepage

    End Sub
However, It seems the HTTP header isn't the right one and I can't seem to find the right one with LiveHTTPHeader or Fiddler, any tips to find the right one?

My current HTTP header =
Code:
"referer=http%253A%252F%252Fwww." & NsTextBox3.Text & ".seafight.bigpoint.com%252Fusername%253D" & NsTextBox1.Text & "%2526password%253D" & NsTextBox2.Text
Or maybe a good tutorial with good explanation.

Thanks in advance.