0

I am developing an application where Logging In returns a cookie named "authCookie" from server and this cookie is in header of the response. I am using Volley library and String Request for server-mobile application communication. Can you please guide me how do I get this cookie from header?

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) { 
                    try {
                        JSONObject obj = new JSONObject(response);
                        if(obj.has("csrf") && obj.has("refreshToken")){
                            csrf = obj.getString("csrf");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();

                    NetworkResponse networkResponse = error.networkResponse;
                    if (networkResponse != null){
                        if(error.networkResponse.statusCode == 400){
                            Toast.makeText(MainActivity.this,"Invalid username/password",Toast.LENGTH_LONG).show();
                        }
                    }
                    else{
                        Toast.makeText(MainActivity.this,"Check internet connection!",Toast.LENGTH_LONG).show();
                    }
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("username", username);
            params.put("password", password);
            return params;
        }

   };

    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);

I want authCookie from response and save it in a string.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • search before asking questions. this is a very general question. well check this => https://stackoverflow.com/questions/36430958/getting-headers-from-a-response-in-volley – Harkal Aug 20 '19 at 15:01
  • You might need to include the Volley source in your project rather than referencing the Volley .jar file. When Volley calls the StringRequest.onResponse() method, it probably has the entire HTTPResponse object available, which you could query for the header info. – Michael Dougan Aug 20 '19 at 18:51
  • 1
    @HarKal I had gone through every answer,but couldn't resolve my problem. Thanks for link, it gave me idea on how to resolve it. – Anand Kumar Sep 01 '19 at 07:27

0 Answers0