美好的一天!凌空忽略Cookie標頭要求
我的應用程序使用簡單的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));
}
}
之後,我要求我的設備列表,與當前用戶進行聯繫。在這一刻,我從響應頭文件中找到了cookei。完整標題上AUTH請求響應輸出:
- 緩存控制 - 無店內,無緩存,必重新驗證,檢查後= 0,預檢查= 0
- 連接 - 備存─ alive
- Content-Length - 16
- Content-Type - text/html;字符集= UTF-8
- 日期 - 週五,2015年9月18日6點15分57秒GMT
- 失效 - 週四,1981年11月19日8點52分00秒GMT
- 語用 - 無緩存
- 服務器 - nginx
- Set-Cookie - PHPSESSID = osurmkq1fmnj462c3hk76l1405;路徑=/
- X-Android的接收-的毫秒時間 - 1442553247146
- X-Android的發送-的毫秒時間 - 1442553247096
- X供電-通過 - PHP/27年3月5日
在驗證請求onResponce回調我採取設置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到設備列表請求的頭信息,但作爲迴應,我看到這一點:{「地位」:假,「錯誤」:「沒有授權」}
在臺式電腦上的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);
}
IMO,不是因爲Cookie頭,而是getParams(),你應該爲POST參數重寫getBody()。看看[我的答案在這裏](http://stackoverflow.com/questions/32600209/error-sending-data-to-server-using-volley-library/32634261#32634261) – BNK
我沒有使用POST請求 –
請嘗試http://stackoverflow.com/questions/16626032/volley-post-get-parameters和https://gist.github.com/mstfldmr/f6594b2337e3633673e5 – BNK