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

    Default Session problem while trying to login automatically into app using httpunit API

    /*

    Hi,

    I need help regarding the following code that I have tried to use. I'm trying to login into an application automatically by running this code using httpUnit API. I have a login page where I'm required to input username & password & also have a submit button. I'm successfully able to do this but when the form is submitted & when my I.E. 7 / Chrome 8 browser opens up, it doesn't redirect to the main page after logging-in. Instead, it gives an error "your session has been expired,please login again" which is an application generated error. I tried to debug with various statements & came to know that I'm being successfully authenticated & reaching the right page when i apply getText() methods but the session is getting expired. Also when i check the header information, it is displaying same JSESSION ID every time for the cookie . I'm not able to understand how to rectify this problem.


    The code is working fine. You need to use the following jar files: httpunit-1.6.jar, Tidy.jar, js.jar & xmlParserAPIs.jar. And also u'll have to give a login page url with the form name along with the username & password values (their form names also) to set their values in the form.

    I request you all to help me out with the same & give me a solution since I need to get through it quickly.


    */






    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.net.URL;

    import com.meterware.httpunit.GetMethodWebRequest;
    import com.meterware.httpunit.HttpUnitOptions;
    import com.meterware.httpunit.WebConversation;
    import com.meterware.httpunit.WebForm;
    import com.meterware.httpunit.WebRequest;
    import com.meterware.httpunit.WebResponse;
    import com.meterware.httpunit.ClientProperties;
    import com.meterware.httpunit.cookies.CookieListener;
    import com.meterware.httpunit.cookies.CookieProperties;

    import com.meterware.httpunit.HttpInternalErrorException;

    public class Test2 {
    public static void main(String[] args) {
    try {

    // Here's the browser
    WebConversation wc = new WebConversation();

    CookieProperties.setDomainMatchingStrict(false);
    CookieProperties.setPathMatchingStrict(false);

    // Don't throw exceptions when JavaScript errors occur
    HttpUnitOptions.setExceptionsThrownOnScriptError(f alse);

    HttpUnitOptions.setLoggingHttpHeaders(true);

    ClientProperties cltprops = wc.getClientProperties();
    //cltprops.setUserAgent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/HTTPUnit");
    cltprops.setAcceptGzip(false);
    cltprops.setAutoRedirect(true);
    cltprops.setAcceptCookies(true);
    cltprops.setAutoRefresh( true );


    CookieProperties.addCookieListener(new CookieListener() {
    public void cookieRejected(String s, int i, String s1) {
    System.out
    .println("CookieProperties.isDomainMatchingStrict( ) = "
    + CookieProperties.isDomainMatchingStrict());
    System.out
    .println("CookieProperties.isPathMatchingStrict() = "
    + CookieProperties.isPathMatchingStrict());
    System.out.println("s = " + s);
    System.out.println("s1 = " + s1);
    System.out.println("Reason: " + i);
    }
    });

    //here input your own login page address

    WebRequest req = new GetMethodWebRequest(
    "http://yourloginpage.com");

    req.setHeaderField("ORA_WX_SESSION", "10.1.0.34:47873-2#3");
    req.setHeaderField("Keep-Alive","300");
    req.setHeaderField("Connection","keep-alive");
    req.setHeaderField("JSESSIONID", "0a01002230d7760c3064f5d54d71b35e84bd606926fc.e3aO aNuPbhiQe3uMc3mPbNaRbO0");

    req.setParameter("formar_SUBMIT","1");
    req.setParameter("jsf_sequence","5");
    req.setParameter("formar:_link_hidden_","");
    req.setParameter("pCodigo","02011000");
    req.setParameter("formar:_idcl","formar:tree:0:0:0 :parter2");

    // Fetch a page
    WebResponse resp = wc.getResponse(req);


    // here input the name of the login form
    WebForm form = resp.getFormWithName("LoginForm");

    System.out.println(wc.getCookieValue("JSESSIONID") );
    String[] cookies = wc.getCookieNames();
    for (int i = 0; i < cookies.length; i++) {
    String cooky = cookies[i];
    System.out.println("");
    System.out.println(cooky + " = " + wc.getCookieValue(cooky));
    wc.putCookie(cooky, wc.getCookieValue(cooky));


    }
    //wc.set_connectTimeout();

    //here input the username & password parameters with correct parameter names

    form.setParameter("username", "admin");
    form.setParameter("password", "admin");


    System.out.println(form.getParameterValue("usernam e"));


    form.submit();


    for (int i = 0; i < cookies.length; i++) {
    String cooky = cookies[i];
    System.out.println(cooky + " = " + wc.getCookieValue(cooky));
    wc.putCookie(cooky, wc.getCookieValue(cooky));

    }


    resp = wc.getCurrentPage();
    System.out.println(resp.getResponseMessage() + " " + resp.getURL() + " " + resp.getContentType());
    //System.out.println(resp.getText());

    //WebForm [] forms=resp.getForms();

    URL url=resp.getURL();

    /*
    System.out.println(CookieListener.ACCEPTED);
    System.out.println(wc.getHeaderField("JSESSIONID") );
    System.out.println(wc.getCookieValue("cooky"));
    */
    // System.out.println(wc.getHeaderFields(url));

    for (int i = 0; i < cookies.length; i++) {
    String cooky = cookies[i];
    System.out.println(cooky + " = " + wc.getCookieValue(cooky));
    wc.putCookie(cooky, wc.getCookieValue(cooky));

    }

    openURL(resp.getURL());

    //System.out.println(resp.getText());
    }
    catch(HttpInternalErrorException ne)
    {
    System.out.println(ne.getMessage());
    }

    catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void openURL(URL url) throws ClassNotFoundException,
    NoSuchMethodException, IllegalAccessException,
    InvocationTargetException, IOException, InterruptedException {
    String osName = System.getProperty("os.name");



    if (osName.startsWith("Mac OS")) {
    Class fileMgr = Class.forName("com.apple.eio.FileManager");
    Method openURL = fileMgr.getDeclaredMethod("openURL",
    new Class[] { String.class });
    openURL.invoke(null, new Object[] { url.toString() });
    } else if (osName.startsWith("Windows")) {
    String cmdLine = "rundll32 url.dll,FileProtocolHandler "
    + url.toString();
    Process exec = Runtime.getRuntime().exec(cmdLine);
    exec.waitFor();
    } else { // assume Unix or Linux
    String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
    "mozilla", "netscape" };
    String browser = null;
    for (int count = 0; count < browsers.length && browser == null; count++) {
    if (Runtime.getRuntime().exec(
    new String[] { "which", browsers[count] }).waitFor() == 0) {
    browser = browsers[count];
    }
    }
    if (browser == null) {
    throw new IllegalStateException("Could not find web browser");
    } else {
    Runtime.getRuntime().exec(
    new String[] { browser, url.toString() });
    }
    }
    }

    }



    //Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

Similar Threads

  1. Login page problem
    By fox in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 05-04-2010, 11:30 AM
  2. ActionListener run automatically problem
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 10:42 PM
  3. Replies: 2
    Last Post: 12-23-2008, 07:35 PM
  4. Replies: 0
    Last Post: 06-25-2008, 01:41 PM
  5. Problem with login
    By adeeb in forum AWT / Swing
    Replies: 0
    Last Post: 06-08-2008, 09:44 AM

Posting Permissions

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