HttpClient cookie的问题

 今天想写个模拟web登陆的压力测试代码,首先想到的是 httpclient 因为做压力测试没必要模拟页面操作,只要对server造成loading就行了。

公司的系统架构是SSH的,我的想法是先用n个client登陆SSO,然后把n个client扔到n个线程,然后n个线程一起start.
开始一直报CircularRedirectException的exception. debug发现一直在尝试登陆。
就怀疑是cookie 功能有问题,因为用IE登陆会在cookie 目录生成cookie 文件,但 是模拟登陆却没有。
中间的问题研究就不多说了,结论并不是cookie 的问题,而是httpclient executeMethod时会判断执行过的action再执行就抛出CircularRedirectException。
解决方法就是重写啦。

重载CustHttpMethodDirector

processRedirectResponse function remark掉下面这段
/*            if (this.redirectLocations.contains(redirectUri)) {
                throw new CircularRedirectException("Circular redirect to '" +
                    redirectUri + "'");
            }*/

代码贴出来供参考。
[code=java]
private static final Log log = LogFactory.getLog(Test.class);

    public void test() throws InterruptedException {       
        System.setProperty("apache.commons.httpclient .cookiespec","COMPATIBILITY");
        List userList = initUserList();
        List userList2 = initUserList2();
        List userActionList = new ArrayList();
       
        //init group 1
        for (Iterator iter = userList.iterator(); iter.hasNext();) {
            String empNo = (String) iter.next();
            HttpClient client = userLogin(empNo);
            userActionList.add(getUserAction1(empNo,client));
        }
        //init group 2
        for (Iterator iter = userList2.iterator(); iter.hasNext();) {
            String empNo = (String) iter.next();
            HttpClient client = userLogin(empNo);
            userActionList.add(getUserAction2(empNo,client));
        }
       
        log.info("---------------------start action------------------------");
        // start batch do
        for (Iterator iter = userActionList.iterator(); iter.hasNext();) {
            UrlOperation urlOperation = (UrlOperation) iter.next();
            urlOperation.start();
        }
        Thread.sleep(100000000);
    }
   
    public UrlOperation getUserAction1(String empNo,HttpClient client){
        List parameters = new ArrayList();
        return new UrlOperation(empNo, "http://fffff:8086/sys/fff/todo.action", null, client);
    }
   
    public UrlOperation getUserAction2(String empNo,HttpClient client){
        List parameters = new ArrayList();
        parameters.add(new NameValuePair("bomFormId", "181"));
        return new UrlOperation(empNo, "http://fffff:8086/sys/fff/viewBomRequestForm.action", parameters, client);
    }
   
    private List initUserList() {
        List list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        list.add("eee");
        return list;
    }
   
    private List initUserList2() {
        List list = new ArrayList();
        list.add("aaaa");
        list.add("aaaaaa");
        list.add("bbbbbbb");
        list.add("vvvvv");
        return list;
    }

    public HttpClient userLogin(String empId) {
        HttpClient client = getClient();
        List parameters = new ArrayList();
        parameters.add(new NameValuePair("userLoginId", empId));
        parameters.add(new NameValuePair("password", "111111"));
        doInClient(empId, "http://aaaaaa:8888/sso/login.action", parameters, client);         return client;
    }

    public void doInClient(String empId, String url, List parameterList, HttpClient client) {
        Date date = new Date();
        GetMethod authpost = new GetMethod(url);

        // Prepare login parameters
        if (parameterList != null)
            authpost.setQueryString((NameValuePair[]) parameterList.toArray(new NameValuePair[parameterList.size()]));
        authpost.setFollowRedirects(true);
        try {
            client.executeMethod(authpost);
            //log.info(authpost.getResponseBodyAsString());
            Cookie [] cookies=client.getState().getCookies();
            client.getState().addCookies(cookies);
            authpost.releaseConnection();

        } catch (HttpException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }
        System.out.println("Action " + url + " run time:" + ((new Date()).getTime() - date.getTime()));
    }

    public HttpClient getClient() {
        CustHttpClient client = new CustHttpClient();
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 20);
        return client;
    }

    class UrlOperation extends Thread {

        private String url;// 
        private String empId;
        private List parameterList = new ArrayList();
        private HttpClient client;

        public UrlOperation(String empId, String url, List parameters, HttpClient client) {
            this.url = url;
            this.empId = empId;
            if (parameters != null)
                parameterList.addAll(parameters);
            this.client = client;
        }

        public void run() {
            Date date = new Date();
            GetMethod get = new GetMethod(url);

            get.setQueryString((NameValuePair[]) parameterList.toArray(new NameValuePair[parameterList.size()]));
            get.setFollowRedirects(true);
            try {
                client.executeMethod(get);
                //log.info(get.getResponseBodyAsString());
                get.releaseConnection();
            } catch (HttpException e) {
                log.error(e);
            } catch (IOException e) {
                log.error(e);
            }
            System.out.println("Action " + url + " run time:" + ((new Date()).getTime() - date.getTime()));
        }
    }

你可能感兴趣的:(apache,thread,IE,ssh,SSO)