Ilias Ilias - 2 years ago 732
Android Question

Get cookie from website using volley

I'm trying to get an identification cookie sent from a website, and for this i'm using Volley library.

First of all, yes i have searched this subject extensively for the past couple of days and using all the answers i managed to rack up this code:

public class Login extends AppCompatActivity {
StringRequest stringRequest = null;
RequestQueue queue;

protected void onCreate(Bundle savedInstanceState) {
queue = Volley.newRequestQueue(this);
Button b = (Button) findViewById(;
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final String url = "";
stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
public void onResponse(String response) {
CookieManager manager = new CookieManager();
try {
Log.d("URI 1", manager.getCookieStore().get(new URI("")).toString());
} catch (URISyntaxException e) {
Log.d("URIS", manager.getCookieStore().getURIs().toString());
Log.d("COOKIE", manager.getCookieStore().getCookies().toString());
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// Add the request to the RequestQueue.



Which does not return anything more than empty brackets, so i'm guessing something(s) wrong..
For starters am i heading the right way here? and what exactly do i need to add to make this work?

Answer Source

you dont need to put this:

CookieManager manager = new CookieManager();

in your response. but only once before you start making calls.

Using CookieHandler HttpURLConnection, which apparently is the http stack you use :), takes care of the cookies so you need to take care of that in Volley itself.

so you can do

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

just before queue = Volley.newRequestQueue(this);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download