1

我正在开发一个用于在移动设备上移植业务应用程序的应用程序。关于 cookie,我将它们存储在 sharedPreferences 中,当应用程序启动时,检查此项,如果存在 cookie,放入 cookieStore,然后将 cookieStore 放入 httpContext _myContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);。此上下文作为 httpClient.execute(request, myContext) 中的参数发送。现在,我在调试模式下检查上下文、cookie、cookieStore 响应 ecc 和 ALL VALUES 的值是否正确,但是服务器响应我,因为我还没有登录。有人可以帮助我吗?这里是管理 cookie 的代码:

私有静态 HttpContext handleCookieStore() {

try {

        if (_myContext == null ) {

            _myContext = new BasicHttpContext();
        }

        // Instantiate and fill cookieStore
        CookieStore cookieStore = (CookieStore)_myContext.getAttribute(ClientContext.COOKIE_STORE);

        if ( cookieStore == null ) {

            // Instantiate new coockieStore
            cookieStore = new BasicCookieStore();


            // Instantiate utility class
            Utility utility = new Utility(_sharedPref);

            Map<String, ?> sessionCookie = utility.getSessionCookie();

            // Look for existing cookies into sharing pref
            if( sessionCookie != null ) {

                // Instantiate a map of string for iterate on cookie's values saved in sharedPref
                Map<String, ?> cookies = sessionCookie;

                // Take all the keys
                Object [] keys = cookies.keySet().toArray();

                for (int i=0; i<cookies.size(); i++ ) { 

                    String name = (String) keys[i];
                    String value = (String) cookies.get(keys[i]);

                    Cookie cookie = new BasicClientCookie( name , value );

                    cookieStore.addCookie(cookie);
                }

                // Set store to context
                _myContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            } else {
                //

                _myContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
            }

        } else {
            Utility utility = new Utility(_sharedPref);

            // Read the existing cookies
            List<Cookie> cookies = cookieStore.getCookies();

            for(Cookie cookie : cookies) {

                System.out.println("c = " + cookie.getName() + " : " + cookie.getValue());

                // Save cookie value into shared preferences
                utility.saveSessionCookie(cookie.getName(), cookie.getValue() );
            }

        }

} catch (Exception e) {

    Log.e("exception->CustomHttpClient.handleCookie", " " + e);
    return null;
}
// Return current context
return _myContext;

}

在此先感谢大家!

4

0 回答 0