android - Volley 忽略 Cookie 头请求

标签 android android-volley androidhttpclient android-cookiemanager

美好的一天!

我的应用程序使用简单的 http 客户端-服务器通信,在客户端使用 Volley 库建立。首先,我启动授权请求:

public class AuthenticationRequest extends StringRequest {

    private static final String TAG = AuthenticationRequest.class.getSimpleName();

    private String login;
    private String password;

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

    public void setLogin(String login) {
        this.login = login;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    protected Map<String, String> getParams() {
        HashMap<String, String> parameters = new HashMap<>();
        parameters.put("login", login);
        parameters.put("password", password);
        return parameters;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        for (Map.Entry<String, String> entry : response.headers.entrySet()) {
            Log.d(TAG, entry.getKey() + " -- " + entry.getValue());
        }

        String sessionId = response.headers.get("Set-Cookie");
        return Response.success(sessionId, HttpHeaderParser.parseCacheHeaders(response));
    }
}

紧接着我请求我的设备列表,与当前用户相关联。此刻我有一个来自响应 header 的 cookei。身份验证请求的完整 header 响应输出:

  • 缓存控制——无存储、无缓存、必须重新验证、post-check=0、pre-check=0
  • 连接——保持 Activity 状态
  • 内容长度 -- 16
  • 内容类型——文本/html;字符集=utf-8
  • 日期 -- 2015 年 9 月 18 日星期五 06:15:57 GMT
  • 到期 -- 1981 年 11 月 19 日星期四 08:52:00 GMT
  • 编译指示 -- 无缓存
  • 服务器 -- nginx
  • 设置 Cookie -- PHPSESSID=osurmkq1fmnj462c3hk76l1405;路径=/
  • X-Android-Received-Millis -- 1442553247146
  • X-Android-Sent-Millis -- 1442553247096
  • X-Powered-By -- PHP/5.3.27

在 Auth 请求的 onResponce 回调中,我将 Set-Cookie 值作为 sessionId 并启动 DeviceListRequest:

public class DeviceListRequest extends StringRequest {

    private static final String TAG = DeviceListRequest.class.getSimpleName();

    private String phpSessId;

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

    public void setPhpSessId(String phpSessId) {
        this.phpSessId = phpSessId;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<>();
        Log.d(TAG, phpSessId);
        headers.put("Cookie", phpSessId);
        return headers;
    }
}

我在这里将 sessionId 放到设备列表请求的 header 中,但作为响应我看到了这个:{"status":false,"error":"No authorization"}

在桌面 PC 上的 Chrome 浏览器中按预期接收设备列表 - 问题出在 Android 上。

拜托,你能告诉我,我的代码有什么问题吗?也许我在设置标题时出错了???

发送请求程序代码:

public void authenticationRequest(String login, String password) {
        final AuthenticationRequest request = new AuthenticationRequest(Request.Method.GET,
                AUTHENTICATION_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Response: " + response);
                        phpSessId = response;
                        deviceListRequest(phpSessId);
                    }
                },
                this);

        request.setLogin(login);
        request.setPassword(password);

        requestQueue.add(request);
    }

    public void deviceListRequest(String phpSessId) {
        DeviceListRequest request = new DeviceListRequest(Request.Method.GET,
                DEVICE_LIST_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Response: " + response);
                    }
                },
                this);

        request.setPhpSessId(phpSessId);

        requestQueue.add(request);
    }

最佳答案

如果您不需要与 Android < 2.3 的兼容性,您只需在 Activity 或应用程序的 onCreate 中添加这行代码。这将为所有 httpURL 连接激活默认的 cookieManager。

CookieHandler.setDefault(new CookieManager());

希望这对您有所帮助。

如果您需要更多的向后兼容性,您还需要为 HttpClient 管理 Cookie

编辑:在后一种情况下,请按照此答案传递自定义 HttpClientStack:https://stackoverflow.com/a/21271347

关于android - Volley 忽略 Cookie 头请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645823/

相关文章:

java - 使用 Volley 缓存图片

php - 使用 Curl (SinglePayment) 验证移动支付

android - 获取带有改造的网站的 html - Android?

android - 在 android 中将 View 捕获为图像时出错

android - 尝试使用 getBuyIntentExtraParams 升级订阅的 IAP Android 问题不返回 BUY_INTENT 键

android - 如何在应用内调用支付网关?

json - 如何在 Kotlin 的 Volley 请求中在 Url 中添加正文?

android - 使用 android 发出 HTTP 请求

android - 如何隐藏 url 请求

android - 无法在 ExpandableListView 子项中键入 EditText