• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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.

  • 3
1 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 = 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 :-)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now