我有以下问题,我试图解决但无法控制。
我正在使用Android 4.4.2登录PHP。服务器(php)返回PHPSESSID,我存储该会话cookie以进行进一步的通信。
现在,有时在App的一些不活动之后,我得到一个HTTP 401,因为显然cookie或会话被拒绝了。但是,当我重新登录时,有时我没有获得新Cookie,所以我陷入了HTTP 401循环中。当发生这种情况时,我就会杀死App的PID。当然这对用户体验不利。
这是要登录的应用代码。
public static Response authenticate(String login, String passwd, boolean relogin, Context context)
throws ApplicationException {
// Log.v("Login", "Going to authenticate with username : " + login);
// Log.v("Login", "LOGIN URI : " + LOGIN_URI);
username = login;
password = passwd;
String text = null;
int responseCode = 0;
try {
// set session timeout
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, HttpUtil.SESSION_TIMEOUT);
HttpPost httpPost = new HttpPost(LOGIN_URI);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = HttpUtil.getClient().execute(httpPost, HttpUtil.getContext());
Header[] headers = httpResponse.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
// Header header = headers[i];
// Log.v("Login", "Header name: " + header.getName());
// Log.v("Login", "Header value: " + header.getValue());
}
Header[] cookieHeader = httpResponse.getHeaders("Set-Cookie");
if (cookieHeader.length > 0) {
HttpUtil.setPHPSESSID(cookieHeader[0].getValue(), "Login");
} else {
// NO COOKIE, KILL APP
if (relogin) {
Log.e("Login", "relogin failed, killing app.");
// coming from login but failed
alertDialog = new AlertDialog.Builder(context);
alertDialog
.setTitle("Login")
.setMessage(
"Je sessie is verlopen, de app wordt nu gesloten. Start daarna opnieuw op om in te loggen.")
.setIcon(R.drawable.ic_launcher);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}
}
responseCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity entity = httpResponse.getEntity();
text = HttpUtil.getASCIIContentFromEntity(entity);
// Log.v("Login", "AUTHENTICATE_RESPONSE_CODE: " + responseCode);
// Log.v("Login", "AUTHENTICATE_RESPONSE: " + text);
// Log.v("Login", "COOKIE: " + HttpUtil.getPHPSESSID());
Response response = new Response();
response.setHttpCode(responseCode);
response.setMessage(text);
return response;
} catch (Exception e) {
e.printStackTrace();
throw new ApplicationException(e.getMessage());
}
}
因此,当我在上面的代码中打印标题时,没有cookie(??)。
这里是要登录的缩短的PHP代码
<?php
require_once '../php/actionutils.php';
// start session
if (!isset($_SESSION)) {session_start();
}
login("APP");
// return userid to the app
echo $_SESSION['userid'];
?>
获取cookie后,所有POST都会通过此代码。 PHP端检查是否设置了userid $ _SESSION var,如果没有,则会销毁会话并返回401.
public static Response httpPost(String uri,
List<NameValuePair> nameValuePairs) throws ApplicationException {
String text = null;
int responseCode = 0;
try {
// set session timeout
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
HttpUtil.SESSION_TIMEOUT);
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Cookie", HttpUtil.PHPSESSID);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = HttpUtil.getClient().execute(httpPost,
HttpUtil.getContext());
responseCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity entity = httpResponse.getEntity();
text = HttpUtil.getASCIIContentFromEntity(entity);
Log.v("HttpPost", uri + " RESPONSE_CODE: " + responseCode);
Log.v("HttpPost", uri + " RESPONSE: " + text);
Log.v("HttpPost", "COOKIE: " + HttpUtil.PHPSESSID);
Response reponse = new Response();
reponse.setHttpCode(responseCode);
reponse.setMessage(text);
return reponse;
} catch (Exception e) {
e.printStackTrace();
throw new ApplicationException(e.getMessage());
}
}
我PHP返回401我被重定向到登录屏幕以进行重新登录。这就是问题出现的地方。在某些场合,重新登录并没有给我一个新的Cookie。