Solved

Login to my favorite web site

Posted on 2011-02-22
15
410 Views
Last Modified: 2012-06-27
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
Comment
Question by:investalert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 10

Expert Comment

by:cdebel
ID: 34954114
Are you using the WebBrowser component?
0
 

Author Comment

by:investalert
ID: 34954267
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
 
LVL 10

Expert Comment

by:cdebel
ID: 34954497
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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 10

Expert Comment

by:cdebel
ID: 34954520
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
 

Author Comment

by:investalert
ID: 34954726
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
 

Author Comment

by:investalert
ID: 34954807
By the way, I did add the webBrowser control to my Form, if that makes any difference.
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34954815
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
 

Author Comment

by:investalert
ID: 34955015
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
 

Author Comment

by:investalert
ID: 34979185
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
 

Author Comment

by:investalert
ID: 34979208
Here is the next web page I need to go to:

 https://www.XXXX.com/inet/ent_logon/Logon
0
 

Author Comment

by:investalert
ID: 34979378
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
 
LVL 10

Accepted Solution

by:
cdebel earned 250 total points
ID: 34979452
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
 
LVL 10

Expert Comment

by:cdebel
ID: 34979484
I've a meeting to prepare, so i'll be back in 2h.
0
 

Author Comment

by:investalert
ID: 34980489
OK, got all the bugs worked out and web page logged on and navigate to correct page. Thanks for your help.
0
 

Author Closing Comment

by:investalert
ID: 34980517
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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error import assembly 19 24
Import a excel sheet in a grid 2 45
Run software updates from the website 6 50
What is GIS method of Geometry data type? 6 33
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question