Thursday, June 8, 2017

How to put Cookie session id into volley request?

Leave a Comment

So, i have this code to make a POST request with volley:

public void myRequest() {  RequestQueue queue = Volley.newRequestQueue(this);  String url = "http://bid.cbf.com.br/a/bid/carregar/json/";  StringRequest postRequest = new StringRequest(Request.Method.POST, url,   new Response.Listener <String> () {    @Override    public void onResponse(String response) {     Log.d("Response", response);    }   },   new Response.ErrorListener() {    @Override    public void onErrorResponse(VolleyError error) {     Log.d("ERROR", "Error => " + error.toString());    }   }  ) {   @Override   public byte[] getBody() throws AuthFailureError {    String httpPostBody = "uf=PE&dt_pesquisa=23%2F05%2F2017&tp_contrato=TODOS&n_atleta=&codigo_clube=&exercicio=";    return httpPostBody.getBytes();   }    @Override   public Map <String, String> getHeaders() throws AuthFailureError {    Map <String, String> params = new HashMap <String, String> ();    params.put("User-Agent", "Mozilla/5.0");    params.put("Accept", "application/json, text/javascript, */*; q=0.01");    params.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");    //params.put("Cookie", "PHPSESSID=ra0nbm0l22gsnl6s4jo0qkqci1"); PROBLEM HERE    return params;   }  };   queue.add(postRequest); } 

If I send a valid cookie ID this return a valid JSON object else a empty object.

I tried (unsuccessfully) to set default cookie handles like

CookieManager manager = new CookieManager(); CookieHandler.setDefault(manager);

but I get a empty object.

How to put a valid cookie session ID to post request?

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment