Problem to simulate the browser.(user httpclient or httpunit)

Discussions

General J2EE: Problem to simulate the browser.(user httpclient or httpunit)

  1. Hi,all
       I have some problem to simulate the browser when I use httpclient of apache,or httpunit.
       The thing is there r 2 urls in the server(based on Notes)
       the first url is http://132.159.173.27/dept1/public/homepage.nsf/HomePage?OpenForm,
       the second url is http://132.159.173.27/dept1/public/sign_in.nsf?OpenDatabase,
    the visitor will not visit the 2nd url if he doesn`t log in the system through the 1st url.
    when I try to simulate the browser by httpclient.
    here is my code:
         String url ="http://132.159.173.27/dept1/public/homepage.nsf/HomePage?OpenForm";//the 1st url
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        client.setConnectionTimeout(800);
        postMethod.addParameter("%%ModDate","0000000000000000");
        postMethod.addParameter("Username",userName);
        postMethod.addParameter("Password",password);
        postMethod.addParameter("RedirectTo","/dept1/public/homepage.nsf/HomePage?OpenForm");
        int statusCode1 = client.executeMethod(postMethod);
        System.out.println("statusCode1="+statusCode1);
        url="http://132.159.173.27/dept1/public/sign_in.nsf?OpenDatabase";//the 2nd url
        //HttpMethod method = new GetMethod(url);
        postMethod = new PostMethod(url);
        int statusCode = client.executeMethod(postMethod);
        System.out.println("statusCode="+statusCode);
        System.out.println(postMethod.getResponseBodyAsString());
        //method.releaseConnection();
        //System.out.println("statusLine>>>" + postMethod.getStatusLine());
        postMethod.releaseConnection();

    When I run these codes,the first step I will log in the system,but when I try to visit url2 through httpclient,the
    response will resend to the 1st url to login automatically,
    so What is the reason?? thx.


    When I try to use httpunit,insted of httpclient,the thing will be different.
    here is my code:
    FormLogin login = new FormLogin("login");
        String username = login.getUserName();
        String password = login.getPassword();
        WebConversation webConversation = new WebConversation();
        try {
          WebResponse response = webConversation.getResponse(
              "http://132.159.173.27/dept1/public/homepage.nsf/HomePage?OpenForm");//the first url
          //get the form to login,and u can change it depends on the real system
          //to simulate the form of oa login system
          WebForm form = response.getForms()[0];
          //WebForm form = response.getFormWithName("_DominoForm");
          form.setParameter("Username", username);
          form.setParameter("Password", password);
          form.setParameter("%%ModDate", "0000000000000000");
          form.setParameter("RedirectTo",
                            "/dept1/public/homepage.nsf/HomePage?OpenForm");
          //submit the form request
          response = form.submit();
          WebLink[] link = response.getLinks();
          int j = 0;
          for (int i = 0; i < link.length; i++) {
            if (link[i].getURLString().equalsIgnoreCase(
                "../../public/sign_in.nsf?OpenDatabase"))
              j = i;
          }
          //response = webConversation.getResponse("http://132.159.173.27/dept1/public/sign_in.nsf/_k2f7t84uano9rrs8jp3ah7i5b2epbu_?OpenForm");// the second url
          //System.out.print(link[j].getURLString());
          response = link[j].click();
          // System.out.println(response.getContentType());
          // System.out.println(response.getText());
        }
        catch (Exception e) {
          e.printStackTrace();
        }

    when the program run to "response = link[j].click();"
    and the exception will be thrown,just like this:
    TypeError: getElementsByTagName is not a function. (httpunit; line 36)

    at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:597)

    at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:557)

    at org.mozilla.javascript.NativeGlobal.typeError1(NativeGlobal.java:567)

    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2178)

    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2163)

    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:58)

    at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.performEvent(JavaScript.java:172)

    at com.meterware.httpunit.scripting.ScriptableDelegate.doEvent(ScriptableDelegate.java:56)

    at com.meterware.httpunit.WebResponse$Scriptable.load(WebResponse.java:689)

    at com.meterware.httpunit.javascript.JavaScript.load(JavaScript.java:89)

    at com.meterware.httpunit.javascript.JavaScriptEngineFactory.load(JavaScriptEngineFactory.java:58)

    at com.meterware.httpunit.RequestContext.runScripts(RequestContext.java:44)

    at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:122)

    at com.meterware.httpunit.WebWindow.sendRequest(WebWindow.java:110)

    at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:253)

    at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:232)

    at com.meterware.httpunit.WebLink.click(WebLink.java:98)

    at org.alex.FormLogin.main(FormLogin.java:64)

    com.meterware.httpunit.ScriptException: Event 'transformView()' failed: TypeError: getElementsByTagName is not a function. (httpunit; line 36)

    at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.handleScriptException(JavaScript.java:202)

    at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.performEvent(JavaScript.java:175)

    at com.meterware.httpunit.scripting.ScriptableDelegate.doEvent(ScriptableDelegate.java:56)

    at com.meterware.httpunit.WebResponse$Scriptable.load(WebResponse.java:689)

    at com.meterware.httpunit.javascript.JavaScript.load(JavaScript.java:89)

    at com.meterware.httpunit.javascript.JavaScriptEngineFactory.load(JavaScriptEngineFactory.java:58)

    at com.meterware.httpunit.RequestContext.runScripts(RequestContext.java:44)

    at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:122)

    at com.meterware.httpunit.WebWindow.sendRequest(WebWindow.java:110)

    at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:253)

    at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:232)

    at com.meterware.httpunit.WebLink.click(WebLink.java:98)

    at org.alex.FormLogin.main(FormLogin.java:64)


    Can anyone to help me??
    Thanks very much.
  2. Good old JavaScript[ Go to top ]

    I can't see your pages (timeout), but i've never been able to use httpunit on any page that uses a significant amount of JavaScript. If you can avoid executing the javascript, go for it... (your errors come from the js)

    sv