排球忽略Cookie标头请求 [英] Volley ignores Cookie header request
问题描述
美好的一天!
我的应用程序使用简单的http客户端-服务器通信,该通信是通过客户端Volley库建立的.首先,我启动授权请求:
My app uses simple http client-server communication, established with Volley library on client side. First, i fire up an authorization request:
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));
}
}
此后,我立即请求与当前用户相关联的设备列表.这时我有一个来自响应头的Cookei.完整的标头响应授权请求输出:
Right after that i requests i list of devices, assiciated with current user. At this moment i have a cookei from response headers. Full headers response output on auth request:
- 缓存控制-不存储,不缓存,必须重新验证,后检查= 0,预检查= 0
- 连接-保持活动状态
- 内容长度-16
- Content-Type-文本/html; charset = utf-8
- 日期-2015年9月18日星期五06:15:57 GMT
- 已过期-格林尼治标准时间1981年11月19日星期四08:52:00
- 编译指示-无缓存
- 服务器-nginx
- Set-Cookie-PHPSESSID = osurmkq1fmnj462c3hk76l1405;路径=/
- X-Android-Received-Millis-1442553247146
- X-Android-Sent-Millis-1442553247096
- X-Powered-By-PHP/5.3.27
- Cache-Control -- no-store, no-cache, must-revalidate, post-check=0, pre-check=0
- Connection -- keep-alive
- Content-Length -- 16
- Content-Type -- text/html; charset=utf-8
- Date -- Fri, 18 Sep 2015 06:15:57 GMT
- Expires -- Thu, 19 Nov 1981 08:52:00 GMT
- Pragma -- no-cache
- Server -- nginx
- Set-Cookie -- PHPSESSID=osurmkq1fmnj462c3hk76l1405; path=/
- X-Android-Received-Millis -- 1442553247146
- X-Android-Sent-Millis -- 1442553247096
- X-Powered-By -- PHP/5.3.27
在Auth请求的onResponce回调中,我将Set-Cookie值用作sessionId并启动DeviceListRequest:
in onResponce callback of Auth request i take Set-Cookie value as sessionId and fire up a 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放在设备列表请求的标头中,但作为响应,我看到了这个:{"status":false,"error":没有授权"}
Here i put sessionId to headers of device list request, but in response i see this: {"status":false,"error":"No authorization"}
在台式机的Chrome浏览器中,设备列表显示为错误-问题出在Android上.
In Chrome browser on Desktop PC recieves list of devices as expeceted - the problem is on Android.
请,您能告诉我,我的代码有什么问题?也许我在设置标题上错了??
Please, Can you tell me, what's the problem with my code? Maybe i'm wrong at setting header???
发送请求过程的代码:
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您只需要在活动或应用程序的onCreate中添加此行代码.这将为所有httpURL连接激活默认的cookieManager.
If you do not need compatibility with Android < 2.3 you just need to add this line of code in your onCreate of the activity or the application. That will activate default cookieManager for all httpURLconnections.
CookieHandler.setDefault(new CookieManager());
希望获得帮助.
如果您需要更多的向后兼容性,则还需要对HttpClient进行Cookie管理
If you need more backwards compatibility you need Cookie management also for HttpClient
在后一种情况下,请按照以下答案传递自定义HttpClientStack: https://stackoverflow.com/a/21271347
in this latter case follow this answer to pass a custom HttpClientStack: https://stackoverflow.com/a/21271347
这篇关于排球忽略Cookie标头请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!