• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

Login to my favorite web site

Looking for code to login to my favorite https:// web site using my userID and PW. I am experienced in vba and VB6, but new to vs2010 and ASP.NET. I've been studying these newer programming techniques, but need help in getting started in ASP.NET. I can find the tags in my web site using IE8 and F12 to search or outline the DOMs and pick out the userID, PW, and submit if button omitted by using innerText. Your help is greatly appreciated.
Note: I've tried other code snippets found on the web but they appear to be for earlier versions of .NET and give me errors when pasted in VS2010.
0
investalert
Asked:
investalert
  • 9
  • 6
1 Solution
 
Christian de BellefeuilleProgrammerCommented:
Are you using the WebBrowser component?
0
 
investalertAuthor Commented:
I can but,... I have struggled with building a Windows Form Application orASP.NET Web Application in VB 2010. I believe it is in my best interest to build it in ASP.NET using Web Application for future expansion. However, I am open to suggestions.
0
 
Christian de BellefeuilleProgrammerCommented:
This is how i've done my login in the past, using a WebBrowser.  You probably can do that without putting the object on a form...

 
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        If Not bSendPasswordPostBack Then
            Dim sUserNameField As String = "ctl00_ContentPlaceHolder1_UserName"
            Dim sUserPasswordField As String = "ctl00_ContentPlaceHolder1_Password"
            Dim sSubmitButton As String = "ctl00_ContentPlaceHolder1_btnLogin"

            bSendPasswordPostBack = True
            WebBrowser1.Document.GetElementById(sUserNameField).InnerText = "MyUserName"
            WebBrowser1.Document.GetElementById(sUserPasswordField).InnerText = "MyPassword"
            WebBrowser1.Document.GetElementById(sSubmitButton).InvokeMember("click")
        Else
            bSendPasswordPostBack = False
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sMyWebSite As String = "https://yourwebsite.com"

        WebBrowser1.Navigate(sMyWebSite)
    End Sub

Open in new window


What is the web site that you are trying to access?  Because you say that you can't find the tags for the login, but they must be somewhere...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Christian de BellefeuilleProgrammerCommented:
Note: bSendPasswordPostBack is a boolean to make sure that you won't fill up the username & password on 2nd DocumentCompleted.   I've not shown the declaration so here it is:

Private bSendPasswordPostBack As Boolean
0
 
investalertAuthor Commented:
Thanks of the quick reply. I tried it in VS2010 and get the following:

bSendPasswordPostBack is noted with error message "Expression is a value and therefore cannot be the target of an assignment" in If Statement.

And Yes, I have the tags for the userID and PW. They are XXXXNUM and XXXXPass.  

 The HTML is shown below.
<DIV id="logonBox">
<FORM id="Logon" onsubmit="return handleLogonSubmit()" method="post" name="Logon" action="j_security_check">
<INPUT id="fp_syslang" type="hidden" name="fp_syslang" value="" /> <INPUT id="fp_software" type="hidden" name="fp_software" value="" /> <INPUT id="fp_userlang" type="hidden" name="fp_userlang" value="" /> <INPUT id="fp_display" type="hidden" name="fp_display" value="" /> <INPUT id="fp_lang" type="hidden" name="fp_lang" value="" /> <INPUT id="fp_timezone" type="hidden" name="fp_timezone" value="" /> <INPUT id="fp_browser" type="hidden" name="fp_browser" value="" /> <INPUT type="hidden" name="LogonToken" value="" /> <INPUT type="hidden" name="PageType" value="" /> <LABEL id="XXXXNumLabel" for="XXXXNum">Online ID</LABEL> <INPUT onkeydown="ChangeFocus(event);" id="XXXXNum" class="loginField" onkeypress="removeErrorMessageDiv()" aria-labelledby="messageLoginErrorLabel XXXXNumLabel" aria-required="true" maxLength="20" size="25" name="j_username" value="" /> <LABEL for="XXXXPass">Password</LABEL> <INPUT id="XXXXPass" class="loginField" onkeypress="removeErrorMessageDiv()" aria-required="true" maxLength="12" size="25" type="password" name="j_password" value="" /><BUTTON class="nAction" onclick="false" type="submit">Log On</BUTTON> 
<P class="textSmall">
Forgot Your <A href="https://www.XXXX.com/inet/ent_proof/proofingEvent?action=Init&event=forgotOnlineId">Online ID</A> or <A href="https://www.XXXX.com/inet/ent_proof/proofingEvent?action=Init&event=forgotPassword">Password</A>?
</P>
<P class="textSmall">
<A href="https://www.XXXX.com/inet/ent_proof/proofingEvent?action=Init&event=registration"><STRONG>Register with XXXX.com</STRONG></A>
</P>
</FORM>
</DIV>

Open in new window

0
 
investalertAuthor Commented:
By the way, I did add the webBrowser control to my Form, if that makes any difference.
0
 
Christian de BellefeuilleProgrammerCommented:
Sorry for that, as i said, i forgot to include the declaration for bSendPasswordPostBack.
In this sample, my form name is Form1 as you can see.

 
Public Class Form1
    Private bSendPasswordPostBack As Boolean

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        If Not bSendPasswordPostBack Then
            Dim sUserNameField As String = "ctl00_ContentPlaceHolder1_UserName"
            Dim sUserPasswordField As String = "ctl00_ContentPlaceHolder1_Password"
            Dim sSubmitButton As String = "ctl00_ContentPlaceHolder1_btnLogin"

            bSendPasswordPostBack = True
            WebBrowser1.Document.GetElementById(sUserNameField).InnerText = "MyUserName"
            WebBrowser1.Document.GetElementById(sUserPasswordField).InnerText = "MyPassword"
            WebBrowser1.Document.GetElementById(sSubmitButton).InvokeMember("click")
        Else
            bSendPasswordPostBack = False
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sMyWebSite As String = "https://yourwebsite.com"

        bSendPasswordPostBack = False
        WebBrowser1.Navigate(sMyWebSite)
    End Sub
End Class

Open in new window

0
 
investalertAuthor Commented:
Thanks for the reply. Your code goes to the web site and enters the userID and PW. Need to go to meeting and will look at the detail later and respond. Thanks again cedebel!
0
 
investalertAuthor Commented:
OK, got my user name and password entered in the boxes as needed. However, the submit button does not have an ID. I entered the code below and it works, but it causes a circular execution meaning that it enters a continous loop and I don't know how to stop it. Also, when I manually click the submit button on the web page, the web page shows it is trying to navigate to the next page but hangs up. What else do I need to do?
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
        For Each webpageelement As HtmlElement In allelements
            If webpageelement.InnerText = "Log On" Then
                webpageelement.InvokeMember("click")
            End If

Open in new window

0
 
investalertAuthor Commented:
Here is the next web page I need to go to:

 https://www.XXXX.com/inet/ent_logon/Logon
0
 
investalertAuthor Commented:
It appears the "bSendPasswordPostBack = False" is causing a Log Off and then the page reloads and enters the UserID and Password. This is a loop that continues until the close button is clicked or the Stop Debugging is pressed.

The logon page where the UserID and Password are entered is shown below:

https://www.xxxx.com/inet/ent_logon/Logon

The page to navigate to after the logon is complete is:

https://www.XXXX.com/inet/ent_home/CpHome
0
 
Christian de BellefeuilleProgrammerCommented:
Maybe you got to to take the problem in the other way with the bSendPasswordPostBack.

It's there to know if it's time to fill up the user name & password and submit.   But if you keep loading other pages it won't be useful.

In your Form Load, set it to true.  And in DocumentCompleted, remove the "NOT" in the IF statement.  In the IF statement (at line 11 in my last code), set it to FALSE.  And Remove the ELSE statement.  This way it will really happen only ONCE, on startup of your application.

0
 
Christian de BellefeuilleProgrammerCommented:
I've a meeting to prepare, so i'll be back in 2h.
0
 
investalertAuthor Commented:
OK, got all the bugs worked out and web page logged on and navigate to correct page. Thanks for your help.
0
 
investalertAuthor Commented:
Had to feness that coding and sequence to get final results. However, expert's guidance was good based on information I provided, which was somewhat limited due to security of my web site.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now