服务器不从http请求中获取cookie

时间:2013-06-21 14:42:21

标签: java android cookies

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

private static 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;

}

提前感谢所有人!

0 个答案:

没有答案