0
votes

public static ApiFactory getInstance(String url, String userName, String password) {

            String sessionId = null;
            client = new DefaultHttpClient();
            ConstantUtil.setHostUrl(url);
            ConstantUtil.setUserName(userName);
            ConstantUtil.setPassword(password);

            try {
                    request = new HttpGet(ConstantUtil.getLoginUrl());
                    response = client.execute(request);
                    final Header[] headers = response.getHeaders("Set-Cookie");
                    for (int i = 0; i < headers.length; i++) {
                            if (headers[i].toString().indexOf("JSESSIONID") > 0) {
                                    int start = headers[i].toString().indexOf("JSESSIONID");
                                    int end = headers[i].toString().indexOf(";");
                                    sessionId = headers[i].toString().substring(start, end);
                            }
                    }

                    ConstantUtil.setLoginJsessionId(sessionId);



            } catch (ClientProtocolException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            } finally {
                    request.abort();
            }

not able to get the authentication myloginUrl is... String loginurl = ConstantUtil.getHostUrl() + "/j_spring_security_check?j_user="+ getUserName() + "&j_passw=" + getPassword(); actually i need jsessionId which i need for the later purpose when i will post the json data.

2
Could it be because you're setting it after you're getting it? To an invalid-looking constant that may not even exist? - user207421
And why is it wrong?? - Antoniossss
actually this is working code . and it is because i m setting session id manually. but session id i m getting ConstantUtil.setLoginJsessionId(sessionId) here is wrong and this is because i m doing ConstantUtil.setLoginJsessionId("JSESSIONID=mzJzfKOYfK1DKZr4Yiua5g**"); - shrayansh

2 Answers

0
votes

I think you should get JSESSIONID from cookies, insteed of getting it from headers in "magical" way. To do that DefaultHttpClient has something like getCookieStore() and the CookieStore has getCookies() method. Try to use them.

EDIT: Ad comment

You are wrong, and your loging in mechanism is not working properly. If you paste JSESSIONID that represents session of already logged user, server probably ignores login request and returns authenticated content. This is called session hijacking. So you need to double check login mechanism.

0
votes

As Antoniossss said, this it would be better to use the standard cookie handling methods. That being said, your way should probably get you the cookies, since after all they are being send in the form of a header.

However, I do see a potential problem with your code. You test if the index of JSESSIONID is greater than 0. But if JSESSIONID is at the very begining of the header string, than it's index would be 0 and you would miss that. You should instead test if the index of JSESSIONID is greater than -1.