android - 使用 Volley,如果响应具有多个 Cookie,我如何将 Session Cookie 与 StringRequest 一起使用?

标签 android android-volley

我如何使用 StringRequest 发送 session cookie 以识别网络服务器的请求。如果有两个来自服务器的 session cookie?

这是我的代码,

[1] 发起新请求 (StringRequest) , 代码 fragment let,

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {                    
                Map<String, String> headers = super.getHeaders();
                if (headers == null || headers.equals(Collections.emptyMap())) {
                    headers = new HashMap<String, String>();
                }
                try {
                    addSessionCookie(headers);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return headers;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                // check session cookies :: custom method
                try {
                    checkSessionCookie(response.headers);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return super.parseNetworkResponse(response);
            }

[2] 获取 session cookie

// check session cookies
    public final void checkSessionCookie(Map<String, String> headers)
            throws Exception {
        if (headers.containsKey("Set-Cookie")
                && headers.get("Set-Cookie").startsWith("sessionid")) {
            String mycookie = headers.get("Set-Cookie");
            if (mycookie.length() > 0) {
                String[] splitCookie = mycookie.split(";");
                String[] splitSessionId = splitCookie[0].split("=");
                mycookie = splitSessionId[1];

            }
        }
    }

    // add session Cookie
    public final void addSessionCookie(Map<String, String> headers)
            throws Exception {
        if (mycookie.length() > 0) {
            StringBuilder builder = new StringBuilder();
            builder.append("sessionid");
            builder.append("=");
            builder.append(mycookie);
            if (headers.containsKey("Cookie")) {
                builder.append("; ");
                builder.append(headers.get("Cookie"));
            }
            headers.put("Cookie", builder.toString());
        }
    }

但上面的代码不起作用,响应是“授权错误”。

如何从响应中获取 session cookie(可能不止一个)并在新请求中设置,以验证用户?

最佳答案

这应该有效:

/**
 * Created by shaharb on 5/18/15.
 */
public class DStringRequest extends StringRequest {
    public DStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    public DStringRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
    }

    /* (non-Javadoc)
         * @see com.android.volley.Request#getHeaders()
         */
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = super.getHeaders();
        if (headers == null || headers.equals(Collections.emptyMap())) {
            headers = new HashMap<>();
        }
        // add the session cookie
        // try to get the cookie from the shared prefs
        String sessionId = Hawk.get("connect.sid", "");
        if (sessionId.length() > 0) {
            StringBuilder builder = new StringBuilder();
            builder.append("connect.sid");
            builder.append("=");
            builder.append(sessionId);
            if (headers.containsKey("Set-Cookie")) {
                builder.append("; ");
                builder.append(headers.get("Set-Cookie"));
            }
            headers.put("Set-Cookie", builder.toString());
        }

        return headers;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        // since we don't know which of the two underlying network vehicles
        // will Volley use, we have to handle and store session cookies manually
        if (headers.containsKey("Set-Cookie")
                && headers.get("Set-Cookie").startsWith("connect.sid")) {
            String cookie = headers.get("Set-Cookie");
            if (cookie.length() > 0) {
                String[] splitCookie = cookie.split(";");
                String[] splitSessionId = splitCookie[0].split("=");
                cookie = splitSessionId[1];

                // Store is somewhare, shared prefs is ok
                Hawk.put("connect.sid", cookie);
            }
        }

        return super.parseNetworkResponse(response);
    }
}

关于android - 使用 Volley,如果响应具有多个 Cookie,我如何将 Session Cookie 与 StringRequest 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28778380/

相关文章:

Android Wear - 通知标题格式

JAVA Jackson 解析包含列表/数组的 JSON 响应

java - 通过串行接收到奇数字符 ☐[J

android - 循环内的 Volley 请求

android - 为什么 volley 库不能在 android 9(API 28) 上工作,而在 android 8(API 27) 上工作正常?

android - 清除 AdMob 在 Android 中生成的缓存文件是一种好习惯吗?

带有双破折号的 Android XML 注释

android - ProGuard 在编译发布 apk 时找不到类

android volley 验证 ssl 证书

php - 如何使用指定参数的 Android Volley GET 方法?