: how to supply login/password to a web site

Discussion in 'Java' started by zigzagdna, Feb 17, 2012.

  1. zigzagdna

    zigzagdna Guest

    I have a web site which I want to access from a java program.
    Problem is web site requires a login and password. However web site
    Does not provide in URL a way to specify login and password.
    Is there any way in my java progam I can supply various key strokes
    which I will
    Type when I logon manually.

    Thanks,
     
    zigzagdna, Feb 17, 2012
    #1
    1. Advertising

  2. zigzagdna

    Roedy Green Guest

    On Thu, 16 Feb 2012 17:53:24 -0800 (PST), zigzagdna
    <> wrote, quoted or indirectly quoted someone who
    said :

    >I have a web site which I want to access from a java program.
    >Problem is web site requires a login and password. However web site
    >Does not provide in URL a way to specify login and password.
    >Is there any way in my java progam I can supply various key strokes
    >which I will

    This is much easier than you would imagine.

    see http://mindprod.com/jgloss/authentication.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    One of the most useful comments you can put in a program is
    "If you change this, remember to change ?XXX? too".
     
    Roedy Green, Feb 17, 2012
    #2
    1. Advertising

  3. zigzagdna

    Arne Vajhøj Guest

    On 2/16/2012 8:53 PM, zigzagdna wrote:
    > I have a web site which I want to access from a java program.
    > Problem is web site requires a login and password. However web site
    > Does not provide in URL a way to specify login and password.
    > Is there any way in my java progam I can supply various key strokes
    > which I will
    > Type when I logon manually.


    Typical the web site will use form based login.

    In which case you will need to first send a POST to the
    action URL of the login page and then request the stuff you
    need using the session cookie you got back from the login.

    I will strongly recommend Apache HttpClient over raw
    (Http)URLConnection.

    I do have some examples on the shelf if you are interested.

    And if it is a ASP.NET web side using view stat, then you
    will have some extra challenges.

    Arne
     
    Arne Vajhøj, Feb 17, 2012
    #3
  4. zigzagdna

    zigzagdna Guest

    On Feb 16, 9:16 pm, Arne Vajhøj <> wrote:
    > On 2/16/2012 8:53 PM, zigzagdna wrote:
    >
    > > I have a web site which I want to access from a java program.
    > > Problem is web site requires a login and password. However web site
    > > Does not provide in URL a way to specify login and password.
    > > Is there any way in my java progam I can supply various key strokes
    > > which I will
    > > Type when I logon manually.

    >
    > Typical the web site will use form based login.
    >
    > In which case you will need to first send a POST to the
    > action URL of the login page and then request the stuff you
    > need using the session cookie you got back from the login.
    >
    > I will strongly recommend Apache HttpClient over raw
    > (Http)URLConnection.
    >
    > I do have some examples on the shelf if you are interested.
    >
    > And if it is a ASP.NET web side using view stat, then you
    > will have some extra challenges.
    >
    > Arne

    Thanks so much, internet newsgroups are so helpful.
     
    zigzagdna, Feb 17, 2012
    #4
  5. zigzagdna

    zigzagdna Guest

    On Feb 16, 9:16 pm, Arne Vajhøj <> wrote:
    > On 2/16/2012 8:53 PM, zigzagdna wrote:
    >
    > > I have a web site which I want to access from a java program.
    > > Problem is web site requires a login and password. However web site
    > > Does not provide in URL a way to specify login and password.
    > > Is there any way in my java progam I can supply various key strokes
    > > which I will
    > > Type when I logon manually.

    >
    > Typical the web site will use form based login.
    >
    > In which case you will need to first send a POST to the
    > action URL of the login page and then request the stuff you
    > need using the session cookie you got back from the login.
    >
    > I will strongly recommend Apache HttpClient over raw
    > (Http)URLConnection.
    >
    > I do have some examples on the shelf if you are interested.
    >
    > And if it is a ASP.NET web side using view stat, then you
    > will have some extra challenges.
    >
    > Arne


    Thanks a lot for your expert advise, internet newsgroups are so
    helpful.
     
    zigzagdna, Feb 17, 2012
    #5
  6. zigzagdna

    zigzagdna Guest

    On Feb 16, 9:47 pm, zigzagdna <> wrote:
    > On Feb 16, 9:16 pm, Arne Vajhøj <> wrote:
    >
    >
    >
    >
    >
    > > On 2/16/2012 8:53 PM, zigzagdna wrote:

    >
    > > > I have a web site which I want to access from a java program.
    > > > Problem is web site requires a login and password. However web site
    > > > Does not provide in URL a way to specify login and password.
    > > > Is there any way in my java progam I can supply various key strokes
    > > > which I will
    > > > Type when I logon manually.

    >
    > > Typical the web site will use form based login.

    >
    > > In which case you will need to first send a POST to the
    > > action URL of the login page and then request the stuff you
    > > need using the session cookie you got back from the login.

    >
    > > I will strongly recommend Apache HttpClient over raw
    > > (Http)URLConnection.

    >
    > > I do have some examples on the shelf if you are interested.

    >
    > > And if it is a ASP.NET web side using view stat, then you
    > > will have some extra challenges.

    >
    > > Arne

    >
    > Thanks a lot for your expert advise, internet newsgroups are so
    > helpful.- Hide quoted text -
    >
    > - Show quoted text -


    I do have some examples on the shelf if you are interested.
    >

    Can you post your examples in this thread, else please send them to
    me at . My yahoo mail is decatvated.

    Thanks a lot
     
    zigzagdna, Feb 17, 2012
    #6
  7. zigzagdna

    Arne Vajhøj Guest

    On 2/17/2012 7:35 AM, zigzagdna wrote:
    > On Feb 16, 9:47 pm, zigzagdna<> wrote:
    >> On Feb 16, 9:16 pm, Arne Vajhøj<> wrote:
    >>> On 2/16/2012 8:53 PM, zigzagdna wrote:
    >>>> I have a web site which I want to access from a java program.
    >>>> Problem is web site requires a login and password. However web site
    >>>> Does not provide in URL a way to specify login and password.
    >>>> Is there any way in my java progam I can supply various key strokes
    >>>> which I will
    >>>> Type when I logon manually.

    >>
    >>> Typical the web site will use form based login.

    >>
    >>> In which case you will need to first send a POST to the
    >>> action URL of the login page and then request the stuff you
    >>> need using the session cookie you got back from the login.

    >>
    >>> I will strongly recommend Apache HttpClient over raw
    >>> (Http)URLConnection.

    >>
    >>> I do have some examples on the shelf if you are interested.


    > Can you post your examples in this thread, else please send them to
    > me at


    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;

    public class Login {
    private HttpClient client;
    public Login() {
    client = new DefaultHttpClient();
    }
    public void login(String url, String userField, String userValue,
    String passField, String passValue) throws Exception {
    List<NameValuePair> nvp = new ArrayList<NameValuePair>();
    nvp.add(new BasicNameValuePair(userField, userValue));
    nvp.add(new BasicNameValuePair(passField, passValue));
    post(url, nvp);
    }
    public String get(String url) throws Exception {
    HttpGet met = new HttpGet(url);
    return EntityUtils.toString(client.execute(met).getEntity());
    }
    public String post(String url, List<NameValuePair> nvp) throws
    Exception {
    HttpPost met = new HttpPost(url);
    if (nvp != null) {
    met.setEntity(new UrlEncodedFormEntity(nvp, HTTP.UTF_8));
    }
    return EntityUtils.toString(client.execute(met).getEntity());
    }
    public static void main(String[] args) throws Exception {
    Login lgi = new Login();
    lgi.get("http://localhost:8080/login/open/test.jsp");
    lgi.login("http://localhost:8080/login/j_security_check",
    "j_username", "userarne", "j_password", "xxxxxx");

    System.out.println(lgi.get("http://localhost:8080/login/open/test.jsp"));
    }
    }

    Arne
     
    Arne Vajhøj, Feb 17, 2012
    #7
  8. zigzagdna

    zigzagdna Guest

    On Feb 17, 4:58 pm, Arne Vajhøj <> wrote:
    > On 2/17/2012 7:35 AM, zigzagdna wrote:
    >
    >
    >
    >
    >
    > > On Feb 16, 9:47 pm, zigzagdna<>  wrote:
    > >> On Feb 16, 9:16 pm, Arne Vajhøj<>  wrote:
    > >>> On 2/16/2012 8:53 PM, zigzagdna wrote:
    > >>>> I have a web site which I want to access from a java program.
    > >>>> Problem is web site requires a login and password. However web site
    > >>>> Does not provide in URL a way to specify login and password.
    > >>>> Is there any way in my java progam I can supply various key strokes
    > >>>> which I will
    > >>>> Type when I logon manually.

    >
    > >>> Typical the web site will use form based login.

    >
    > >>> In which case you will need to first send a POST to the
    > >>> action URL of the login page and then request the stuff you
    > >>> need using the session cookie you got back from the login.

    >
    > >>> I will strongly recommend Apache HttpClient over raw
    > >>> (Http)URLConnection.

    >
    > >>> I do have some examples on the shelf if you are interested.

    > >   Can you post your examples in this thread, else please send them to
    > > me at

    >
    > import java.util.ArrayList;
    > import java.util.List;
    >
    > import org.apache.http.NameValuePair;
    > import org.apache.http.client.HttpClient;
    > import org.apache.http.client.entity.UrlEncodedFormEntity;
    > import org.apache.http.client.methods.HttpGet;
    > import org.apache.http.client.methods.HttpPost;
    > import org.apache.http.impl.client.DefaultHttpClient;
    > import org.apache.http.message.BasicNameValuePair;
    > import org.apache.http.protocol.HTTP;
    > import org.apache.http.util.EntityUtils;
    >
    > public class Login {
    >      private HttpClient client;
    >      public Login() {
    >          client = new DefaultHttpClient();
    >      }
    >      public void login(String url, String userField, String userValue,
    > String passField, String passValue) throws Exception {
    >          List<NameValuePair> nvp = new ArrayList<NameValuePair>();
    >          nvp.add(new BasicNameValuePair(userField, userValue));
    >          nvp.add(new BasicNameValuePair(passField, passValue));
    >          post(url, nvp);
    >      }
    >      public String get(String url) throws Exception {
    >          HttpGet met = new HttpGet(url);
    >          return EntityUtils.toString(client.execute(met).getEntity());
    >      }
    >      public String post(String url, List<NameValuePair> nvp) throws
    > Exception {
    >          HttpPost met = new HttpPost(url);
    >          if (nvp != null) {
    >              met.setEntity(new UrlEncodedFormEntity(nvp, HTTP.UTF_8));
    >          }
    >          return EntityUtils.toString(client.execute(met).getEntity());
    >      }
    >      public static void main(String[] args) throws Exception {
    >          Login lgi = new Login();
    >          lgi.get("http://localhost:8080/login/open/test.jsp");
    >          lgi.login("http://localhost:8080/login/j_security_check",
    > "j_username", "userarne", "j_password", "xxxxxx");
    >
    > System.out.println(lgi.get("http://localhost:8080/login/open/test.jsp"));
    >      }
    >
    > }
    >
    > Arne- Hide quoted text -
    >
    > - Show quoted text -


    Arne:
    Thanks a lot.
     
    zigzagdna, Feb 18, 2012
    #8
    1. Advertising

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.

Share This Page