log in to a web page

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.

Who is Participating?
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 = br.read()) != -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
This link will show you how to get the cookie from a connection

That's was quick!

Thanks for the points and the grade :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.