Session problem while trying to login automatically into app using httpunit API
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Unhappy 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.

    Code:
    */

    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(false);

    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.e3aOaNuPbhiQe3uMc3mPbNaRbO0");

    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:0arter2");

    // 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("username"));


    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
    Join Date
    Jan 2011
    Posts
    2

    Unhappy Re: Session problem while trying to login automatically into app using httpunit API

    Could somebody please at least give an idea as to how to go about it...????

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center