-
-
Save lezorich/8f3f3a54f07515881581 to your computer and use it in GitHub Desktop.
This also works for OKHTTP3 + Retrofit 2.
For some reason, other solutions specific to Retrofit is giving me issues with a nodejs using passportjs. The socket.handshake.session does not store passport user even if user is already logged in. I TRIED THIS AND IT WORKED! Session is persistent and can now retrieve passport object from socket session :)
PersistentCookieStore cookieStore = new PersistentCookieStore(context);
CookieManager cookieManager = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
return new OkHttpClient.Builder()
.sslSocketFactory(getSslSocketFactory(caInput), getTrustManager())
.addNetworkInterceptor(new StethoInterceptor())
.addInterceptor(loggingInterceptor)
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();@lezorich, i get a cookie when i login to a website and i need to send that cookie with each request i make. can you please post a sample code for doing the same ?
This is my current code:
RequestQueue queue = VolleySingleton.getInstance(this.getApplicationContext()).
getRequestQueue();
String url = "https://my_api_url";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(">>>>>resp: ", "Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.i(">>>>>resp: ", "Error: " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("aaaa", "dddd");
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
Volley is giving ServorError when this CookieStore is bieng used, If I remove the line to set this, Volley works file but the response returns invalid session.
Do you have any idea why this happens like this?
@lezorich what happens when cookies are removed? Won't it still be stored in the persistent storage?
Hey Lukas,
Thank you for this!
I am facing one issue, though. If I kill the app, and then restart it, I am being logged out. Is this the expected behaviour?
Thank you, again!
Hello, there is another easy way to maintain cookies session and that is to add this line in a class that is extended with APPLICATION class: CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Hello, there is another easy way to maintain cookies session and that is to add this line in a class that is extended with APPLICATION class: CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
@asadullah06 did it work for you ? so cookies are stored automatic and no need to explicitly send them in subsequent requests
Where are you removing cookies from
SharedPreferences? It looks like you remove cookies from theCookieStorebut only fromSharedPreferenceswhen adding a new cookie.