Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Retrofit from clearing my cookies

I get a cookie from my backend API that allows me to authenticate all subsequent user requests. I'm using retrofit, and I can't get it to keep the session key between requests. I want to know how to configure retrofit so that it keeps the session key around and uses it for all future requests:

public class ApiClient{

    private static final String API_URL = "http://192.168.1.25:8080";

    private static RestAppApiInterface sRestAppService;

    public static RestAppApiInterface getRestAppApiClient() {
        if (sRestAppService == null) {

            CookieManager cookieManager = new CookieManager();
            cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            CookieHandler.setDefault(cookieManager);

            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(API_URL)
                    .build();
            sRestAppService = restAdapter.create(RestAppApiInterface.class);
        }
        return sRestAppService;
    }

}
like image 609
frankelot Avatar asked May 27 '14 00:05

frankelot


People also ask

Should you clear your browser cookies?

Cookies do a lot more than just track you, and while clearing them is easy—and sometimes beneficial—doing so can make browsing the web more obnoxious. This is similar to the problem with clearing your cache. If you regularly clear your browser cache, your browsing will be slower. If you regularly clear your cookies, the web will be more annoying.

How do I clear cookies from favorites?

Internet Options> Browsing history> Delete> check "Preserve Favorites website data" before clearing Cookies. Make sure that cookies you want to keep belong to sites in Favorites. Was this reply helpful? Sorry this didn't help.

What happens if you block cookies on a website?

Block or allow cookies If you don't want sites to store cookies on your PC, you can block cookies. But doing this might prevent some pages from displaying correctly, or you might get a message from a site letting you know that you need to allow cookies to view that site.

What happens to your search history when you delete cookies?

In fact, it may make the price go up as it changes based on demand, as airlines and travel websites will use your search history against you. Cookies are pieces of information that store the details of your web browsing, so deleting those effectively deletes your search. "Cookie tracking is real" a web application developer assures us on Quora.


2 Answers

You need to set a Cookie persistent Client. Since you're using Android and retrofit I suggest using OKHttp wich is better supported by retrofit and Android thread safe, the way to this is the following

//First create a new okhttpClient (this is okhttpnative)
OkHttpClient client = new OkHttpClient(); //create OKHTTPClient
//create a cookieManager so your client can be cookie persistant
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager); //finally set the cookie handler on client

//OkClient is retrofit default client, ofcourse since is based on OkHttClient
//you can decorate your existing okhttpclient with retrofit's okClient
OkClient serviceClient = new OkClient(client);

//finally set in your adapter
RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("Some eNdpoint")
            .setClient(serviceClient)
            .build();

The point of using Okhttp instead of the defaultHttpClient(by apache) is that okhttp is threadsafe for android and better supported by retrofit.

Remembar that if you create another adapter you will need to set the same client, perhaps if you implement singleton on the client instance you will use the same one for all your requests, keeping in the same context

I hope this helps,best

like image 183
Jorch914 Avatar answered Oct 26 '22 22:10

Jorch914


If you use Retrofit 2 you can add the library:

compile "com.squareup.okhttp3:okhttp-urlconnection:3.2.0"

then use the following code to manage cookies when you create your OkHttp client:

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cookieJar(new JavaNetCookieJar(cookieManager));
like image 20
Shilaghae Avatar answered Oct 26 '22 22:10

Shilaghae