Results 1 to 9 of 9
  1. #1
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default Login to Websites via Java

    Hey everyone, I am having major problems with logging into a website via Java. I want to log on to this site what requires a userID and but not password and send the html from this page into my program for inspection. I have written this so far. But when I call this class in using my main class it just gives me the html of the login page. Can anyone help me out??
    I have tried using Apache httpclient but dont understand at all how to use it so if you suggest I should use that please give me a demo of how to do so.

    I got most of the code below from this site Login to a website using HTTP POST in Java - Full Java Code | Our website now yours! - Currenlty Java focussed.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    
    public class InputID {
    	
    	
    	private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
    	private static final String LOGIN_ACTION_NAME = "Submit";
    	private static final String LOGIN_USER_NAME = "09006285";
    	private static final String LOGIN_USER_NAME_PARAMETER_NAME = "T1";
    	private static final String TARGET_URL = "http://www.timetable.ul.ie/tt1.asp";
    
    	private String readResponse(HttpURLConnection urlConnection)
    			throws IOException {
    
    		BufferedReader bufferedReader = null;
    		try {
    			// Prepare a reader to read the response from the URLConnection
    			// throws IOException
    			bufferedReader = new BufferedReader(new InputStreamReader(
    					urlConnection.getInputStream()));
    			String responeLine;
    
    			// Good Practice: Use StringBuilder in this case
    			StringBuilder response = new StringBuilder();
    
    			// Read until there is nothing left in the stream
    			// throws IOException
    			while ((responeLine = bufferedReader.readLine()) != null) {
    				response.append(responeLine);
    			}
    
    			return response.toString();
    		} catch (IOException ioException) {
    			System.out.println("Problems while reading the response");
    			ioException.printStackTrace();
    
    			// throw the exception so that the caller is aware that
    			// there was some problems
    			throw ioException;
    
    		} finally {
    			// Good practice: clean up the connections and streams
    			// to free up any resources if possible
    			if (bufferedReader != null) {
    				try {
    					// throws IOException
    					bufferedReader.close();
    				} catch (Throwable ignore) {
    					// Cannot do much with exceptions doing clean up
    					// Ignoring all exceptions
    				}
    			}
    
    		}
    	}
    
    	public HttpURLConnection doHttpPost(String targetUrl, String content)
    			throws IOException {
    		HttpURLConnection urlConnection = null;
    		DataOutputStream dataOutputStream = null;
    		try {
    			// Open a connection to the target URL
    			// throws IOException
    			urlConnection = (HttpURLConnection) (new URL(targetUrl)
    					.openConnection());
    
    			// Specifying that we intend to use this connection for input
    			urlConnection.setDoInput(true);
    
    			// Specifying that we intend to use this connection for output
    			urlConnection.setDoOutput(true);
    
    			// Specifying the content type of our post
    			urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);
    
    			// Specifying the method of HTTP request which is POST
    			// throws ProtocolException
    			urlConnection.setRequestMethod("POST");
    
    			// Prepare an output stream for writing data to the HTTP connection
    			// throws IOException
    			dataOutputStream = new DataOutputStream(
    					urlConnection.getOutputStream());
    
    			// throws IOException
    			dataOutputStream.writeBytes(content);
    			dataOutputStream.flush();
    			dataOutputStream.close();
    
    			return urlConnection;
    		} catch (IOException ioException) {
    			System.out.println("I/O problems while trying to do a HTTP post.");
    			ioException.printStackTrace();
    
    			// Good practice: clean up the connections and streams
    			// to free up any resources if possible
    			if (dataOutputStream != null) {
    				try {
    					dataOutputStream.close();
    				} catch (Throwable ignore) {
    					// Cannot do anything about problems while
    					// trying to clean up. Just ignore
    				}
    			}
    			if (urlConnection != null) {
    				urlConnection.disconnect();
    			}
    
    			// throw the exception so that the caller is aware that
    			// there was some problems
    			throw ioException;
    		}
    	}
    
    	private String preparePostContent(String loginUserName)
    			throws UnsupportedEncodingException {
    		// Encode the user name and password to UTF-8 encoding standard
    		// throws UnsupportedEncodingException
    		String encodedLoginUserName = URLEncoder.encode(loginUserName, "UTF-8");
    
    		String content = "B1=" + LOGIN_ACTION_NAME +" &" + LOGIN_USER_NAME_PARAMETER_NAME +"="
            + encodedLoginUserName;
    		return content;
    
    	}
    
    	public void httpPostLogin() {
    
    		try {
    			// Prepare the content to be written
    			// throws UnsupportedEncodingException
    			String urlEncodedContent = preparePostContent(LOGIN_USER_NAME);
    
    			HttpURLConnection urlConnection = doHttpPost(TARGET_URL,
    					urlEncodedContent);
    
    			String response = readResponse(urlConnection);
    
    			System.out.println("Successfully made the HTPP POST.");
    			System.out.println("Recevied response is: '/n" + response + "'");
    			
    
    		} catch (IOException ioException) {
    			System.out.println("Problems encounterd.");
    
    		}
    
    	}
    
    }


    My main Class is as follows

    Java Code:
    
    public class Studentpage {
    
    	
    	public static void main(String[] args) throws Exception  {
    		
    		
    		
    		InputID ID= new InputID();
    		ID.httpPostLogin();
    		
    		
    
    	
    	}
    }
    Last edited by Toirniuil; 10-01-2010 at 11:34 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default

    First off use code tags (it's the # button) otherwise there's no formatting and I don't read unformatted code.

    Second can you post the main() code as well to see how this is called?

  3. #3
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    OK I edited it to your specifications

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default

    Right, do you know that tt1.asp takes the user name parameter?
    Because looking at the source code for that page it posts to tt2.asp.

    In other words, it's not tt1.asp that does the logon, but tt2.asp.

  5. #5
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    To be honest I am only going by the source code myself, tt2 is the page that I am trying to get it see. You type in the ID to tt1 and click submit then it goes to tt2. If you try to go to tt2 without this page it says invalid login.

    So what ur saying is that the actual login is done on tt2 and tt1 is just used to gather the data?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default

    tt1 has the form...the form is submitted to tt2. That's where the POST is going.

  7. #7
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    I tried changing to tt2 but still didnt work. So I downloaded fiddler2 which is a chatcher for html to see what the page was actually posting when I logged on and got T1=09006285&B1=B1 so I was posting the wrong content so tt2. But you were right I should have posted to tt2 not tt1 so thank you very much for that :D

    I shall definitely be on this site again

  8. #8
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    Not sure how to mark this as solved??

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default

    I have no idea how to do that...:)

    Good that it's working now.

Similar Threads

  1. Accessing Data From Websites in Java
    By capitalistpig in forum New To Java
    Replies: 13
    Last Post: 06-21-2010, 10:11 PM
  2. Replies: 2
    Last Post: 05-06-2010, 05:28 AM
  3. Open up websites
    By PhQ in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 04:01 PM
  4. Best Websites to learn Java?
    By Meta in forum New To Java
    Replies: 21
    Last Post: 03-18-2010, 09:45 AM
  5. Replies: 0
    Last Post: 06-25-2008, 12:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •