Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Username/password logging in

Thread view: 
mark - 15 Nov 2006 13:36 GMT
Hello,

I want to create an app which allows me to log into
https://reg.racingpost.co.uk/cde/login_iframe_rp.sd
I am using jakarta httpMethod to connect but have no idea how to set
username & password to get the correct login page ( I tried with the
methods on
http://jakarta.apache.org/commons/httpclient/authentication.html but
with no success :///). Could you just show me direction in which I
should go? Thx!

Best wishes, Maka
Arne Vajhøj - 16 Nov 2006 01:06 GMT
> I want to create an app which allows me to log into
> https://reg.racingpost.co.uk/cde/login_iframe_rp.sd
[quoted text clipped - 4 lines]
> with no success :///). Could you just show me direction in which I
> should go?

The "authentication" code is for BASIC authentication (and NTLM etc.)
which are HTTP based.

The page looks as if it is using FORM base authentication (which
is session based).

The solution is simply to login.

I have attached an example below.

Arne

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Login {
    private HttpClient client;
    public Login() {
        client = new HttpClient();
    }
    public void login(String url, String userField, String userValue,
String passField, String passValue) throws Exception {
        NameValuePair[] nvp = new NameValuePair[2];
        nvp[0] = new NameValuePair(userField, userValue);
        nvp[1] = new NameValuePair(passField, passValue);
        post(url, nvp);
    }
    public String get(String url) throws Exception {
        GetMethod met = new GetMethod(url);
        try {
            client.executeMethod(met);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return met.getResponseBodyAsString();
    }
    public String post(String url, NameValuePair[] nvp) throws Exception {
        PostMethod met = new PostMethod(url);
        if (nvp != null) {
            met.setRequestBody(nvp);
        }
        try {
            client.executeMethod(met);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return met.getResponseBodyAsString();
    }
    public static void main(String[] args) throws Exception {
        Login lgi = new Login();
        lgi.login("http://www.xxx.dk/login.php", "login_username",
args[0], "login_password", args[1]);
        System.out.println(lgi.get("http://www.xxx.dk/something.php"));
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.