再会!
我的应用程序使用简单的 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));
}
}
在那之后,我请求了与当前用户相关的设备列表。此刻我有一个来自响应头的 cookie。身份验证请求的完整标头响应输出:
- Cache-Control -- 无存储、无缓存、必须重新验证、后检查 = 0、预检查 = 0
- 连接——保持活动
- 内容长度 -- 16
- 内容类型——文本/html;字符集=utf-8
- 日期 -- 格林威治标准时间 2015 年 9 月 18 日星期五 06:15:57
- 到期 -- 格林威治标准时间 1981 年 11 月 19 日星期四 08:52:00
- Pragma -- 无缓存
- 服务器——nginx
- 设置 Cookie -- PHPSESSID=osurmkq1fmnj462c3hk76l1405; 路径=/
- X-Android-Received-Millis -- 1442553247146
- X-Android-Sent-Millis -- 1442553247096
- X-Powered-By -- PHP/5.3.27
在 Auth 请求的 onResponce 回调中,我将 Set-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 放在设备列表请求的标头中,但作为响应,我看到了这个:{“status”:false,“error”:“No authorization”}
在台式 PC 上的 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);
}