Results 1 to 2 of 2
  1. #1
    darkEnergy is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Automatic login, is this the way to do it?

    Hello so I'm just starting to learn how to make java communicate with the internet. So far basically all I can do is read and print out the html coding of a website. Now my goal is to make a program that will log in to a website for me given i provide the right password/username for example facebook or java-forums.

    Reading from and Writing to a URLConnection (The Java™ Tutorials > Custom Networking > Working with URLs)

    After reading through this site especially the part that says//
    "Many HTML pages contain forms — text fields and other GUI objects that let you enter data to send to the server. After you type in the required information and initiate the query by clicking a button, your Web browser writes the data to the URL over the network.
    Many of these HTML forms use the HTTP POST METHOD to send data to the server.
    //

    I feel this is exactly what i needed however the explanation slowly became too much for my understanding and i found another example which made more sense from another site which is the following..

    Java Code:
    public class postMethod {
        public static void main(String[] args) throws Exception {
        	
        	
        	    // Construct data
        	    String data = URLEncoder.encode("username=", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        	    data += "&" + URLEncoder.encode("password=", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    
        	    // Send data
        	    URL url = new URL("http://facebook.com");
        	    URLConnection conn = url.openConnection();
        	    conn.setDoOutput(true);
        	    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        	    wr.write(data);
        	    wr.flush();
    
        	    // Get the response
        	    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        	    String line;
        	    while ((line = rd.readLine()) != null) {
        	        // Process line...
        	    }
        	    wr.close();
        	    rd.close();
        	
        }
    }
    now is this the right path for what i want to do? and if yes I understand the code except for the very beginning the URLEncoder part i don't understand how that works, from what i've read is that this is what links java with the html so from there i went on to sites such as facebook looked at the page source and entered what i thought to be the needed information and my password and username and ran it but nothing seemed to happen. I know i may seem very stupid but I'd like to learn so any direction is very much appreciated thank you.

  2. #2
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default Re: Automatic login, is this the way to do it?

    nothing might not seem to happen because you might haven't defined what should happen.

    when the request is sent to the server, it gives back an HTML page to the application. try doing this:
    (cmd>type 'telnet Google 80') you would see the structured HTML page of Google

    in other words, you have to create an HTML parser to read the HTML returned by the server, which leads you to creating a web browser.
    [why are you annoyed with my sig?]

Similar Threads

  1. automatic ENTER?
    By glina126 in forum New To Java
    Replies: 3
    Last Post: 02-25-2011, 07:08 AM
  2. Automatic sign in?
    By dunafrothint in forum Networking
    Replies: 1
    Last Post: 03-26-2010, 03:00 PM
  3. automatic email
    By pracheebapate in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 08:07 PM
  4. automatic email
    By aswin in forum New To Java
    Replies: 0
    Last Post: 11-13-2008, 08:03 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
  •