到目前为止,我查看了有关此问题的一些问题但没有成功
我想使用Volley库发送带有自定义cookie的http请求
这是我设置cookie的方式:
CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cm);
Map<String,List<String>> header = new HashMap<>();
List<String> values = new ArrayList<>();
values.add("name=value");
header.put("Set-Cookie", values);
try {
cm.put(new URI(uri), header);
}catch (Exception e)
{
Log.e("TEST",e.getMessage().toString());
}
之后我发送此请求:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(BASE_URL +url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
Log.i("TEST", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("TEST", "ERROR: " + error.toString());
}
} ...
AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);
我也尝试在header部分设置cookie,如下所示:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String,String>();
headers.put("Content-Type", "application/json");
headers.put("Cookie", "name=value");
return headers;
}
但是,从服务器端我正在记录请求,似乎没有设置cookie:
验证{cookie: {路径:'/', _expires:null, originalMaxAge:null, httpOnly:true}, 护照:{}}
什么似乎是问题?