Subscribe to Martyr2's Programming Underground        RSS Feed
***** 1 Votes

Login to a Website from Java

Icon 7 Comments
Historically the question "How do I login to a website from X Language?" gets asked from time to time on DIC. The answers to this question has not been the greatest and these questions tend to be largely ignored. I intend to put an end to this for the Java language with a simple example. The answer, while simple, does expect the reader to be at least familiar with how a web form works. We are going to assume the reader knows the page/script of where the form is submitted to and the name of the fields used in the form. In most cases, for a login form, this is simply a username field, password field and a submit button. However, be sure to check the source code of the form to know for sure and make sure all fields are covered when submitting the data from Java. So lets get down to business submitting form data on this episode of the Programming Underground!

<Think of your favorite love song from the 80's, that is the theme music>

So how do we do this? Relatively simple actually if you know of a few Java objects from the java.net namespace. I am talking about the objects URL and URLConnection. We use the URL object to construct a simple URL to where we want to submit our data and also use it to get a URL connection to that page/script. Once we get the connection, we set the connection to a write mode, write to the connection and then read the response... which will typically be our HTML response.

Lets show a simple example...

import java.net.*;
import java.io.*;

public class ConnectToURL {

    // Variables to hold the URL object and its connection to that URL.
    private static URL URLObj;
    private static URLConnection connect;
	
    public static void main(String[] args) {
        try {
            // Establish a URL and open a connection to it. Set it to output mode.
            URLObj = new URL("http://www.examplesite.com/login.php");
            connect = URLObj.openConnection();
            connect.setDoOutput(true);	
        }
        catch (MalformedURLException ex) {
            System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1); 
        }
        catch (Exception ex) {
            System.out.println("An exception occurred. " + ex.getMessage());
            System.exit(1);
        }
		
		
        try {
            // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
            writer.write("username=MyUsername&pass=MyPassword&submit=Login");
            writer.close();
			
            // Now establish a buffered reader to read the URLConnection's input stream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
			
            String lineRead = "";
			
            // Read all available lines of data from the URL and print them to screen.
            while ((lineRead = reader.readLine()) != null) {
                System.out.println(lineRead);
            }
			
            reader.close();
        }
        catch (Exception ex) {
            System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
        }
    }
}




Not a whole lot to this example, which is great for learning the concept! We start by importing our namespaces to get access to URL and URLConnection along with our BufferedReader and BufferedWriter objects. Next we construct our URL object by giving it the URL to where our login form submits to. This is not the URL of where the form is currently located. We want to send the info to the same URL that our form does when we login on a webpage. You can find this information in the source of a page which contains the login form.

If the URL is some how not correct, it will throw a MalformedException when it tries to create the URL. This is typically caused by the wrong protocol being used, a misspelled word or some other error that may pop up from parsing the URL. Next we use this new URL object to open a connection to that URL. It will return our URLConnection object and allow us to get at the underlying streams. Note: To set this connection up for writing, we are going to call the setDoOutput setting to true. If we don't do this, the connection would have output mode set to false and not allow us to write to it.

Once we have the streams, we can grab the write stream with a BufferedWriter (or some other Writer object) and write our form parameters to the URL. In our example we submit a username, password and the login button. These fields will match the field names in your specific login form. Their values will be the values you would have typed in to login. Separate each field=value pair with an ampersand just like you would in a URL. Since we are writing directly to the page/script, this will be registered as a POST which is how most login forms work.

After writing our parameters, we then setup a read of the connection and read back the response (which is typically a HTML page of showing that you are logged in or that login failed). Based on the response content, we must then determine if we did in fact login correctly and then go about fetching pages. That is really all there is to it!

You can use the code provided in the example above to submit data through any web form you like. Just make sure you change the URL and the parameters to match the target web form. I hope you find the code useful. Like all code on the Programming Underground, this code is in the public domain and can be used any way you like. It is the fabulous Programming Underground five finger discount program! :bananaman:/>

Enjoy and thanks again for reading! :)

If you want more blog entries like this, check out the official blog over on The Coders Lexicon. There you will find more code, more guides and more resources for programmers of all skill levels!

7 Comments On This Entry

Page 1 of 1

macosxnerd101 Icon

09 July 2010 - 07:47 PM
Awesome entry, and definitely a neat little trick with wide implications and applications! Thanks for sharing. :)
0

bcranger Icon

10 July 2010 - 12:06 AM
Hmm, I'd like to try this example out! :bigsmile:
1

Martyr2 Icon

10 July 2010 - 10:06 AM
Thanks for the comments guys! I believe this type of entry would have lots of different applications for sure from checking mail to grabbing account info from many social networking sites and bringing them together etc. Someone could make some really nice stuff with something like this. :)
0

evinrows Icon

10 July 2010 - 01:31 PM
Wow, awesome. I didn't even know those objects existed, thanks!
0

hdr Icon

10 July 2010 - 09:09 PM
5 discount program? You, sir, are too kind.
0

d_rop4nme Icon

11 July 2010 - 11:16 AM
Great Code ... I had made something like this in autohotkey but it was unreliable, I have several projects that I've been looking for something like this ... Thanks
3

cilaes Icon

15 January 2012 - 03:54 PM
Next entry you should show your translation to C#!
0
Page 1 of 1

August 2014

S M T W T F S
      1 2
3456789
10111213141516
17181920212223
24252627282930
31