How to login to a site using cookie? (not applet)

Discussion in 'Java' started by anthony.mak@iname.com, Jul 13, 2007.

  1. Guest

    Problem:
    -Want to programmatically (a java program) login to a site, and
    retrieve the next page.
    -Site uses https and POST method.
    -Site uses .aspx

    Can anyone point me to some tutorials or codes that does this please?
    I cannot find any coherent info, different sites say different things
    and
    I cannot get it work...

    Please email to anthony.mak-AT-iname.com

    Anthony
     
    , Jul 13, 2007
    #1
    1. Advertisements

  2. Andrew Thompson, Jul 13, 2007
    #2
    1. Advertisements

  3. Guest

    , Jul 13, 2007
    #3
  4. Roedy Green Guest

    On Fri, 13 Jul 2007 00:34:25 -0000, wrote,
    quoted or indirectly quoted someone who said :

    >Want to programmatically (a java program) login to a site,


    see http://mindprod.com/jgloss/authenticator.html

    You simply write code to provide id/password and Java magically does
    the rest.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 13, 2007
    #4
  5. Roedy Green Guest

    On Fri, 13 Jul 2007 00:34:25 -0000, wrote,
    quoted or indirectly quoted someone who said :

    >-Site uses https and POST method.


    IIRC it is exactly the same as a regular post, except you use a https
    url.

    See http://mindprod.com/products1.html#HTTP
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 13, 2007
    #5
  6. Roedy Green Guest

    On Fri, 13 Jul 2007 14:17:45 -0000, wrote,
    quoted or indirectly quoted someone who said :

    >I am trying to setup a HttpUrlConnection object using:
    >https://www.commsec.com.au/Default.aspx?LoginName=xxx&Password=yyy
    >and URLEncoder.encode() the part after "?"
    >Do I need to treat the password differently, since it is a password
    >input?


    you never send a password in plaintext. Authenticator does it all for
    you.

    http://mindprod.com/jgloss/authentication.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 13, 2007
    #6
  7. Guest

    On Jul 14, 2:24 am, Roedy Green <>
    wrote:
    >
    > seehttp://mindprod.com/jgloss/authenticator.html
    >
    > You simply write code to provide id/password and Java magically does
    > the rest.
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    Thanks Roedy.

    What about Cookie? Firefox stores this cookie called "stn"
    after I logged in, but if I delete this cookie and refresh,
    I will be kicked back to the front login screen. So I think
    it is the session id cookie. Don't I need to send back
    a session id every time I POST?

    Also, how does the Autheticator knows what are the <input>
    names for the username and password? Shouldn't they be specified
    somehow? (for this site, they are "LoginName" and "Password")

    Below is my attempt using the Autheticator, but instead of
    next_page being the string of the next page after a successful login,
    it returned the front page of the site. So the method is not
    successful. Do you have any idea what is wrong?

    Many thanks!

    Anthony

    My Code
    ========
    import java.net.*;
    import java.io.*;

    public class Test2 {

    private String login_url_str = "https://www.comsec.com.au/
    Default.aspx";

    public Test2() {
    }

    public static void main(String[] args) {
    Test2 test1 = new Test2();
    test1.execute();
    }

    private void execute() {
    //Setup for https
    java.security.Security.addProvider(new
    com.sun.net.ssl.internal.ssl.
    Provider());
    System.setProperty("java.protocol.handler.pkgs",
    "com.sun.net.ssl.internal.www.protocol");

    Authenticator.setDefault( new MyAuthenticator() );

    try {
    URL url = new URL(login_url_str);
    //For handling POST request
    String next_page = getURLPostString(url,"");
    System.out.println(next_page);

    } catch (IOException ex) {
    System.out.println(ex);
    System.exit(1);
    }

    }

    /** Post a string to an URL and get the reply as a string. Returns
    an empty
    string if things didn't work out. */
    //code from: http://martin.nobilitas.com/java/cookies.html
    private String getURLPostString(URL url, String body) {
    StringBuffer sb = new StringBuffer();

    // find the newline character(s) on the current system
    String newline = null;
    try {
    newline = System.getProperty("line.separator");
    } catch (Exception e) {
    newline = "\n";
    }

    try {
    // URL must use the http protocol!
    HttpURLConnection conn = (HttpURLConnection)
    url.openConnection();

    conn.setRequestMethod("POST");
    conn.setAllowUserInteraction(false); // you may not ask
    the user
    conn.setDoOutput(true); // we want to send things
    // the Content-type should be default, but we set it
    anyway
    conn.setRequestProperty("Content-type",
    "application/x-www-form-
    urlencoded");
    // the content-length should not be necessary, but we're
    cautious
    conn.setRequestProperty("Content-length",
    Integer.toString(body.length()));

    // get the output stream to POST our form data
    OutputStream rawOutStream = conn.getOutputStream();
    PrintWriter pw = new PrintWriter(rawOutStream);

    pw.print(body); // here we "send" our body!
    pw.flush();
    pw.close();

    // get the input stream for reading the reply
    // IMPORTANT! Your body will not get transmitted if you
    get the
    // InputStream before completely writing out your output
    first!
    InputStream rawInStream = conn.getInputStream();

    // get response
    BufferedReader rdr = new BufferedReader(new
    InputStreamReader(
    rawInStream));
    String line;

    while ((line = rdr.readLine()) != null) {
    sb.append(line);
    sb.append(newline);
    }
    return sb.toString();
    } catch (Exception e) {
    System.out.println("Exception " + e.toString());
    e.printStackTrace();
    }
    return ""; // an exception occurred
    }


    }


    class MyAuthenticator extends Authenticator
    {
    /**
    * Called when password authorization is needed.
    * @return The PasswordAuthentication collected from the
    * user, or null if none is provided.
    */
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication ( "usernameblah",
    "passwordblah".toCharArray() );
    }
    }
     
    , Jul 14, 2007
    #7
  8. Guest

    On Jul 14, 2:25 am, Roedy Green <>
    wrote:
    > Seehttp://mindprod.com/products1.html#HTTP


    How is this relate to the Authenticator you mentioned?
    If I use the Authenticator, do I need to use your "HTTP GET/POST"
    library?

    Anthony
     
    , Jul 14, 2007
    #8
  9. Guest

    On Jul 14, 2:25 am, Roedy Green <>
    wrote:
    > Seehttp://mindprod.com/products1.html#HTTP


    How is this relate to the Authenticator you mentioned?
    If I use the Authenticator, do I need to use your "HTTP GET/POST"
    library?

    Anthony
     
    , Jul 14, 2007
    #9
  10. Andrew Thompson, Jul 14, 2007
    #10
  11. Guest

    On Jul 14, 1:38 pm, "Andrew Thompson" <u32984@uwe> wrote:
    > wrote:
    > >> wrote:
    > >> >-Want to programmatically (a java program) login to a site, ...

    >
    > >> What site? URL?

    > >https://www.commsec.com.au

    >
    > Oh.. *Bastard* Bank! You should have said..
    >
    > --
    > Andrew Thompsonhttp://www.athompson.info/andrew/


    What is the problem with logging into bank?
    I am trying to write a program for my friend, who
    is going to do phd research in economic, that gathers
    stock data for analysis. Is it that much more difficult
    to log into a bank programmatically than let's say Amazon?

    Anthony Mak
     
    , Jul 14, 2007
    #11
  12. wrote:
    > Problem:
    > -Want to programmatically (a java program) login to a site, and
    > retrieve the next page.
    > -Site uses https and POST method.
    > -Site uses .aspx
    >
    > Can anyone point me to some tutorials or codes that does this please?
    > I cannot find any coherent info, different sites say different things
    > and
    > I cannot get it work...


    Use:

    http://jakarta.apache.org/commons/httpclient/

    It takes care of all the details of session state.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jul 22, 2007
    #12
  13. Roedy Green Guest

    Re: How to login to a site using cookie? not applet

    On Sat, 14 Jul 2007 01:32:47 -0000, wrote,
    quoted or indirectly quoted someone who said :
    >What about Cookie?

    Pretty much everything I know about Java I have written up in the Java
    glossary. Just look up plausible keywords and follow links.
    http://mindprod.com
    In this case:
    http://mindprod.com/jgloss/cookie.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 23, 2007
    #13
  14. Roedy Green Guest

    On Sat, 14 Jul 2007 01:39:54 -0000, wrote,
    quoted or indirectly quoted someone who said :

    >How is this relate to the Authenticator you mentioned?
    >If I use the Authenticator, do I need to use your "HTTP GET/POST"
    >library?


    Please read http://mindprod.com/jgloss/authentication.html where I
    explain this at length.

    Authenticator is a Sun class. It is most peculiar the way it works.
    You must register an Authenticator implementation, then forget about
    authentication. It magically kicks in and does its thing as needed to
    log you into a server. You just write you code ignoring logons. You
    need some sort of code like mine to do the interactions with the
    server http://mindprod.com/products1.html#HTTP but you could use
    anything.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 23, 2007
    #14
  15. Roedy Green Guest

    On Sun, 22 Jul 2007 17:31:05 -0400, Arne Vajhøj <>
    wrote, quoted or indirectly quoted someone who said :
    >http://jakarta.apache.org/commons/httpclient/
    >It takes care of all the details of session state.

    The catch is it is about 3 MB. You would not want to download that on
    every Applet. You would want to pre-install it in an EXT directory or
    use Java Web Start so it will be cached.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 23, 2007
    #15
  16. Roedy Green wrote:
    >>http://jakarta.apache.org/commons/httpclient/
    >>It takes care of all the details of session state.

    >The catch is it is about 3 MB. ...


    Not necessarily. I am not familiar with the licence of
    the HTTPClient, but it is technically possible to rejar
    an API so that it only includes the classes immediately
    required. I am betting that will be a lot less than 3 meg.

    >...You would not want to download that on
    >every Applet. ...


    The OP stated (in the subject) 'not applet'.

    >...You would want to pre-install it in an EXT directory or
    >use Java Web Start so it will be cached.


    ..in fact, one might rejar a web start component
    (as HTTPClient might be deployed) ..break it up
    into one jar per package, and deploy them as 'lazy'
    downloads, to let JWS do a somewhat less optimal,
    but much simpler to deploy, option of the launch.

    Not that I agree with helping for this end purpose,
    short of the OP ever getting back to us on a few
    points, but I am just saying..

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
     
    Andrew Thompson, Jul 23, 2007
    #16
  17. Roedy Green wrote:
    > On Sun, 22 Jul 2007 17:31:05 -0400, Arne Vajhøj <>
    > wrote, quoted or indirectly quoted someone who said :
    >> http://jakarta.apache.org/commons/httpclient/
    >> It takes care of all the details of session state.

    > The catch is it is about 3 MB. You would not want to download that on
    > every Applet.


    Did you read the subject line ?

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jul 25, 2007
    #17
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. manu
    Replies:
    1
    Views:
    876
    Evans
    Jul 20, 2008
  2. Replies:
    2
    Views:
    536
  3. Jimbo
    Replies:
    1
    Views:
    733
    Pierre Quentel
    Apr 8, 2010
  4. Eric
    Replies:
    2
    Views:
    694
  5. Paul  Wolpe
    Replies:
    0
    Views:
    193
    Paul Wolpe
    Oct 17, 2005
Loading...

Share This Page