如何获取 volley cookies 并在下一个请求时设置它 - android

问题描述 投票:0回答:1

我使用Django,当用户通过API登录时,发送到Android的响应包含一个包含CSRF和会话的cookie,我需要接收它的值并在下一个请求的标头中发送它们。我尝试了很多,但没有找到解决办法

    private void test() {
        RequestQueue requestQueue;
        requestQueue = Volley.newRequestQueue(this);
        String url_ = "https://xxxx.com/api/BF";
        Map<String, String> params = new HashMap<>();
        params.put("Email", UEmail);
        params.put("Password", UPassword);
        JSONObject JOParams = new JSONObject(params);

        
        JsonObjectRequest JORequest = new JsonObjectRequest(Request.Method.POST, url_, JOParams, response -> {
            try {
                String status_ = response.getString("Status");
                if (status_.equals("OK_")) {
                    Toast.makeText(this, "Login", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Login failed", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                Toast.makeText(this, "Data error", Toast.LENGTH_SHORT).show();
            }
        }, error -> {
            Toast.makeText(this, "Connection failed", Toast.LENGTH_LONG).show();
        });
        requestQueue.add(JORequest);
    }

}

请填写代码。我浏览了之前的所有答案,但没有得到任何结果

java android django django-rest-framework android-volley
1个回答
1
投票

使用下面的代码。

    private void test() {
        RequestQueue requestQueue;
        requestQueue = Volley.newRequestQueue(this);
        String url_ = "https://xxxx.com/api/BF";
        Map<String, String> params = new HashMap<>();
        params.put("Email", UEmail);
        params.put("Password", UPassword);
        JSONObject JOParams = new JSONObject(params);

        
        JsonObjectRequest JORequest = new JsonObjectRequest(Request.Method.POST, url_, JOParams, response -> {
            try {
                String status_ = response.getString("Status");
                if (status_.equals("OK_")) {
                    Toast.makeText(this, "Login", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Login failed", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                Toast.makeText(this, "Data error", Toast.LENGTH_SHORT).show();
            }
        }, error -> {
            Toast.makeText(this, "Connection failed", Toast.LENGTH_LONG).show();
        }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String>  params = new HashMap<>();
                params.put("User-Agent", "BalesApp");
                if(!csrftoken.isEmpty()){
                    String CookeVAL = "csrftoken=" + csrftoken + "; sessionid=" + sessionid + ";";
                    params.put("Cookie", CookeVAL);
                    params.put("X-CSRFToken", csrftoken);
                }


                return params;
            }
            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                int x;
                AHeaders = response.allHeaders;
                for (int n = 0; n< Objects.requireNonNull(AHeaders).size(); n++ ){
                    if(AHeaders.get(n).getName().equals("set-cookie")){
                        HeadVal = AHeaders.get(n).getValue();
                        x = HeadVal.indexOf("csrftoken=");
                        if(x >= 0){
                            int x1 = HeadVal.indexOf("=");
                            int x2 = HeadVal.indexOf(";");
                            csrftoken = HeadVal.substring(x1+1,x2);
                        }

                        x = HeadVal.indexOf("sessionid=");
                        if(x >= 0){
                            int x1 = HeadVal.indexOf("=");
                            int x2 = HeadVal.indexOf(";");
                            sessionid = HeadVal.substring(x1+1,x2);
                        }
                    }
                }

                return super.parseNetworkResponse(response);
            }
        };
        requestQueue.add(JORequest);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.