我按如下方式定义一个截击请求,但有时我无法从服务器获取正确的数据(在我刚登录后),所以我使用Fiddler来查看请求,它看到请求头中有两个Cookie部分,因此服务器将其视为访客标识,并返回错误的数据。所以为什么 那里有两个Cookie部分?
public class JsonArrayPostRequestWithCookie extends Request<JSONArray> {
private static final String TAG = "tag";
public static final String Cookie = "Cookie";
private Map<String, String> mMap;
private Response.Listener<JSONArray> mListener;
private Map<String, String> mSendHeader=new HashMap<String, String>();
public JsonArrayPostRequestWithCookie(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener, Map<String, String> map, String cookie) {
super(Method.POST, url, errorListener);
mListener = listener;
mMap = map;
setSendCookie(cookie);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mMap;
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString =
new String(response.data, HttpHeaderParser.parseCharset(response.headers));
JSONArray jsonarray = new JSONArray(jsonString);
return Response.success(jsonarray,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONArray response) {
mListener.onResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null
|| headers.equals(Collections.emptyMap())) {
headers = mSendHeader;
}else{
headers.remove(Cookie);
headers.putAll(mSendHeader);
}
return headers;
}
private void setSendCookie(String cookie){
if(TextUtils.isEmpty(cookie)){
return;
}
mSendHeader.put(Cookie, cookie);
}
}
请求标头像这样enter image description here,这很奇怪,为什么两个相同的Cookie部分在这里?
PS:这是我如何使用它:
` map.put("vid", "1");
map.put("parent", "0");
map.put("maxdepth", "3");
String cookie = "";
if(Constants.logged){
cookie = StringUtil.getSessionMsg();
}
JsonArrayPostRequestWithCookie getReq = new JsonArrayPostRequestWithCookie(RequestUrlProvider.ALL_MSG_PATH, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
}
}, new Response.ErrorListener() {
}, map, cookie);
getReq.setRetryPolicy(new DefaultRetryPolicy(10000, 0, 1.0f));
getReq.setShouldCache(false);
NYiportalApplication.mReqQue.add(getReq);