Logging in to a site using pure Java code is possible with just the URL and HttpURLConnection classes. First, you need to know the action URL of the <form> tag in a website that you wish to login to. Next, look for all input field tags within the <form> tag because they will be needed to be passed as parameters when you submit to the form URL, e.g. in this case, logging in to the site. Every input field with a name attribute should be passed just to be sure. Suppose we have a sample form that looks like this.
|
1 2 3 4 5 |
<form action="login.jsp"> <input type="text" name="username" size="20"/><br/> <input type="text" name="password" size="20"/><br/> <input type="submit" value="login"/> </form> |
To log in using Java programmatically, use the following code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
URL url = new URL("http://www.mydomain.com/login.jsp"); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); String data = "username=MYUSERNAME&password=MYPASSWORD"; httpcon.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(httpcon.getOutputStream()); wr.write(data); wr.flush(); wr.close(); httpcon.setInstanceFollowRedirects(false); CookieTool ct = new CookieTool(); ct.readCookies(httpcon, false, false); httpcon.disconnect(); |
That is it. Any other page you wish to go to, just use the CookieTool variable which contains everything that the generated cookie contains like session id. What is CookieTool exactly for? Go here to find more about how to use this neat Cookie utility to store your cookie sessions so that which page you would go to the site that requires a login, you do not need to log in again because the cookie information is stored in the CookieTool variable.
Thanks for the example. One thing I found I had to change was that the data string couldn’t use the html code for “&”. In other words,
String data = “username=MYUSERNAME&password=MYPASSWORD”;
otherwise, the server didn’t see the tag “password” in the request, the & either changes the tag to “amp;password”, or loses the tag altogether. I didn’t check which, just that for me removing the “amp;” substring made things work correctly.
Thanks again. This and your other post were a big help to me!
Pat
hey thanks for mentioning that pat. yes you are right. that was my mistake, the code got messed up when i switched from html code view to html view while composing that article. it happens. thanks for letting me know.
glad it helped you. please leave a rating and do subscribe to yahoo and/or google reader. thanks