How to put Cookie session id into volley request?

前端 未结 3 549
谎友^
谎友^ 2021-01-17 09:07

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

public class MainActivity extends AppCompatActivity {

 Button btnSearch;
 ProgressDialog loadingDia         


        
3条回答
  •  既然无缘
    2021-01-17 09:38

    So the problem was getting a valid cookie. My mistake was to get it from the POST request itself. I kept the same working principle, getting the cookie when I started the application but using GET instead of POST and calling the root of the URL instead of the address where I get the JSON.

    My solution looked like this:

    public void requestCookie() {
      queue = Volley.newRequestQueue(this);
      String url = "http://myurl.com/";
    
      StringRequest getRequest = new StringRequest(Request.Method.GET, url,
       new Response.Listener  () {
        @Override
        public void onResponse(String response) {
         String x = myCookieManager.getCookieValue();
        }
       },
       new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
         Log.d("ERROR", "Error => " + error.toString());
         hideLoading();
        }
       }
      ) {   
       protected Response  parseNetworkResponse(NetworkResponse response) {
        try {
         String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
         String header_response = String.valueOf(response.headers.values());
         int index1 = header_response.indexOf("PHPSESSID=");
         int index2 = header_response.indexOf("; path");
         //Log.e(Utils.tag, "error is : " + index1 + "::" + index2);
         session_id = header_response.substring(index1, index2);
    
         return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
         return Response.error(new ParseError(e));
        }
       }
      };
    
      queue.add(getRequest);
     }
    

提交回复
热议问题