使用android volley发送请求并获取响应cookie

时间:2017-04-25 10:19:38

标签: android http request android-volley response

我创建了一个项目并使用REST从服务器发送和获取数据,我使用HttpURLConnection发送请求

然后我发现凌空使其更容易使用,但我在排球时使用cookies时出现问题

这是我的请求函数

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);
}

我有覆盖parseNetworkResponse来获取headerResponse,但我看不到那里的cookie

我的问题是,如何从凌空发送和获取Cookie?

1 个答案:

答案 0 :(得分:0)

尝试此操作并在此方法中传递标题

   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());
            }
        }