Solved

Automatically log into a website in webbrowser component

Posted on 2010-08-22
7
540 Views
Last Modified: 2013-11-27
how to auto logon a site in a webbroswer component. What code will auto populate the username, password input boxes and autoclick the login button?

HTML looks like this:-

<form class="ccform" action="login.php" method="post" onsubmit="login();">
<fieldset>
<legend>Login</legend>
<span class="errormsg-inline"></span>
<div class="field-row">
<label for="username">Username</label>
<input type="text" class="field" name="username" id="username" maxlength="16" value="" tabindex="1" />
</div>
<div class="field-row">
<label for="password">Password</label>
<input type="password" class="field" name="password" id="password" maxlength="16" tabindex="2" />
</div>
<div class="field-row">
<input type="hidden" name="direct" id="direct" value="yes" />
<input type="hidden" name="redirect" value="" />
<input type="submit" class="button-inline" name="submit" value="Login" tabindex="3" />
</div>
<div class="field-row">
<p><a href="public.php?mode=forgot" tabindex="4">I forgot my username or password</a></p>
</div>
</fieldset>
</form>
0
Comment
Question by:Oli999
7 Comments
 
LVL 9

Expert Comment

by:Snarfles
Comment Utility
Is this your own site or someone elses? and how are you opening it?
0
 
LVL 5

Expert Comment

by:kawzaki
Comment Utility
create a POST request to the http://www.example.com/login.php

and that will do it for you.

details

Basically, you would need to do the following:
1. create a HttpWebRequest object and use HttpWebRequest.Method = "POST".
2. set the Post data to contain your data (username and password).
3. "Post" (i.e. execute and get the response for) the HttpWebRequest.


and you will face cookie not enabled issue, so make sure you enable it as described first line in the code below...

CookieContainer cookies = new CookieContainer();
HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(site);
postRequest.CookieContainer = cookies; // note this
postRequest.Method = "POST";
postRequest.ContentType = "application/x-www-form-urlencoded";
using (Stream stream = postRequest.GetRequestStream())
{
    stream.Write(buffer, 0, buffer.Length);
}


good luck!
0
 
LVL 2

Author Comment

by:Oli999
Comment Utility
it's someone else's site and I'm just using webbrowser1.navigate to open it
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
Using IWebBrowser2 is a good idea to do this task.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
             If u need the complete code, plz let me know!
                                             
0
 
LVL 2

Accepted Solution

by:
Oli999 earned 0 total points
Comment Utility
Thanks all for your suggestions. Have figured this one out myself now though.

here's how I did it.
If (WebBrowser1.Document.All("username") IsNot Nothing) And (WebBrowser1.Document.All("password") IsNot Nothing) Then
            WebBrowser1.Document.All("username").SetAttribute("value", "username")
            WebBrowser1.Document.All("password").SetAttribute("value", "password")
            For Each element As HtmlElement In WebBrowser1.Document.Body.All
                If element.Name = "submit" Then
                    If element.GetAttribute("value") = "Login" Then
                        element.InvokeMember("click")
                    End If
                End If
            Next
        End If

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now