log in to a web page

Posted on 2005-04-22
Last Modified: 2010-03-31
I already know how to read the contents of a web page in, but what I would like to do is actually log into a web page with a username and password, and then download the html contents of what comes back.  Anyone know how to take care of this???

I do realize the security concerns with having a password existing in plain text in my java code.

Question by:ARACK04
    LVL 29

    Accepted Solution

    2 ways, both use HttpUrlConnection

    1./ IF the site uses a login cookie that you have in your browser, just add the cookie and go.

    2./ if it doesn't, create a POST request using HttpUrlConnection mimicking the login, then parse the returned login cookie and then create a new request adding the login cookie.

    here's an example method to get you started:

           * Simple connection method using HttpUrlConnection to send a GET
           * request to the location specified by the URL parameter.
           * @param pURL
           * @return String
          private static String getURLContent( final String pURL ){
                StringBuffer sb = new StringBuffer();
                 try {
                      URL u = new URL( pURL );
                      HttpURLConnection huc = (HttpURLConnection) u.openConnection();
                      huc.setRequestMethod( "GET" );
                      //you need value from the persistant cookie from your browser
                      huc.setRequestProperty("Cookie", "Login=blahblah" );
                      //user-agent string
                      huc.setRequestProperty("User-Agent","An example Request property");
                      BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));

                      int b = 0;//read in the HTML returned
                      while((b = != -1) {
                      //close reader
                      //disconnect connection
                      huc.disconnect() ;
                 }catch (IOException e){
                      return "Unable to open connection: " + e.getMessage();
                }//end try/catch
                return sb.toString();
          }//end getURL method
    LVL 29

    Expert Comment

    This link will show you how to get the cookie from a connection
    LVL 29

    Expert Comment

    That's was quick!

    Thanks for the points and the grade :-)

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now