简体   繁体   中英

send request and get response cookies with android volley

I made a project and using REST to send and get data from server, I used HttpURLConnection to send request

then I've found volley that make it easier to use, but I have a problem using cookies on volley

here is my request function

public void doActionJsonPost() {
    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.POST, IConstants.BASE_URL + url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject jsonObject;
                    try {
                        jsonObject = new JSONObject(response);
                        String msgCode = jsonObject.getString("responseCode");
                    } catch (JSONException e) {
                        LoggingHelper.verbose(e.toString());
                        iHttpAsync.onAsyncFailed(e.toString());
                    } catch (Exception e) {
                        LoggingHelper.verbose(e.toString());
                        iHttpAsync.onAsyncFailed(e.toString());
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    LoggingHelper.verbose("ERROR");
                    iHttpAsync.onAsyncFailed(error.getMessage());
                }
            }){
        @Override
        public byte[] getBody() throws AuthFailureError {
            iHttpAsync.onAsyncProgress();
            return parameters.getBytes();
        }
        @Override
        public String getBodyContentType() {
            return "application/json";
        }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return getAuthHeader(context);
        }

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            Map<String, String> responseHeaders = response.headers;
            String rawCookies = responseHeaders.get("Set-Cookie");
            return super.parseNetworkResponse(response);
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);
}

I have override parseNetworkResponse to get headerResponse, but I cannot see cookies there

my question is, how can I send and get cookies from volley?

try this and pass your header in this methods

   public final void checkSessionCookie(Map<String, String> headers) {
            if (headers.containsKey(SET_COOKIE_KEY)
                    && headers.get(SET_COOKIE_KEY).startsWith(SESSION_COOKIE)) {
                    String cookie = headers.get(SET_COOKIE_KEY);
                    if (cookie.length() > 0) {
                        String[] splitCookie = cookie.split(";");
                        String[] splitSessionId = splitCookie[0].split("=");
                        cookie = splitSessionId[1];
                        Editor prefEditor = _preferences.edit();
                        prefEditor.putString(SESSION_COOKIE, cookie);
                        prefEditor.commit();
                    }
                }
        }

        /**
         * Adds session cookie to headers if exists.
         * @param headers
         */
        public final void addSessionCookie(Map<String, String> headers) {
            String sessionId = _preferences.getString(SESSION_COOKIE, "");
            if (sessionId.length() > 0) {
                StringBuilder builder = new StringBuilder();
                builder.append(SESSION_COOKIE);
                builder.append("=");
                builder.append(sessionId);
                if (headers.containsKey(COOKIE_KEY)) {
                    builder.append("; ");
                    builder.append(headers.get(COOKIE_KEY));
                }
                headers.put(COOKIE_KEY, builder.toString());
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM