我如何获得Android排球的响应cookie

时间:2018-04-20 04:47:39

标签: java android android-volley

我发送HTTP请求在Android中使用Volley,重点是在发送请求时,服务器使用具有特定键和值的Set-Cookie进行响应,

我怎样才能获得这个价值?检查浏览器网络我得到以下

enter image description here

我想获取此数值,因为这是后续请求的要求。

这是我目前的代码。

 StringRequest postRequest = new StringRequest(Request.Method.POST, linklogin,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    // response
                    Log.d("Response", response);
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", error.getMessage());
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("user", "");
            params.put("pws", "");

            return params;
        }

    };
    queue.add(postRequest);

2 个答案:

答案 0 :(得分:1)

创建一个名为 VolleyStringRequest 的新类并粘贴以下代码:

import com.android.volley.AuthFailureError;
import com.android.volley.Header;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class VolleyStringRequest implements Response.Listener<String> {
    ResponseListener listener;
    Response.ErrorListener errorListener;
    int method = -1;
    String url;
    List<Header> headers = new ArrayList<>();
    Map<String, String> params;

    @Override
    public void onResponse(String response) {
        if (listener != null) {
            listener.onResponse(response, headers);
        }
    }

    public VolleyStringRequest(int method, String url, ResponseListener listener, Response.ErrorListener errorListener, Map<String, String> params) {
        this.listener = listener;
        this.method = method;
        this.url = url;
        this.errorListener = errorListener;
        this.params = params;

    }

    public VolleyStringRequest(String url, ResponseListener listener, Response.ErrorListener errorListener, Map<String, String> params) {
        this.listener = listener;
        this.url = url;
        this.errorListener = errorListener;
        this.params = params;
    }

    public Request getRequest() {
        if (method == -1) {
            return new Request(url, this, errorListener);
        } else {
            return new Request(method, url, this, errorListener);
        }
    }

    class Request extends StringRequest {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return params;
        }

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

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

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            headers.addAll(response.allHeaders);
            return super.parseNetworkResponse(response);
        }
    }

    public interface ResponseListener {
        void onResponse(String response, List<Header> headers);
    }

}

现在如何使用这个类:

Map<String,String> params = new HashMap<>();
        StringRequest request = new VolleyStringRequest(Request.Method.POST,"https://google.com", new VolleyStringRequest.ResponseListener() {
            @Override
            public void onResponse(String response, List<Header> headers) {
//here you get all the headers. do whatever you want with it
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        },params).getRequest();
      queue.add(request);

答案 1 :(得分:0)

我希望这对你有用。

覆盖此方法。

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