问题
I have successfully setup an Android App which logs-in to my drupal website.
My problem is the logged-in user session does not last very long. The site clearly shows my user as logged-in to the site, but within an hour or so the user is no longer shown as active on the site. (I am guessing because I don't really exactly know it.)
Can anyone offer an insight into why this is happening?
The code is as follows:
protected HttpResponse doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://mystestsite.com/testpoint/user/login");
// TODO Auto-generated method stub
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add( new BasicNameValuePair("password", "guest") );
nameValuePairs.add( new BasicNameValuePair("username", "guest") );
httpPost.setEntity( new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP post request
response = httpClient.execute(httpPost);
Log.i("SEEMS TO WORK", response.toString());
Log.v("CODE", httpPost.getRequestLine().toString() + " - " + response.toString());
}catch(Exception e){
Log.e("HTTP ERROR", e.toString());
}
return response;
}
回答1:
So it sounds like it's an issue with how you're setting your cookies. When I do this kind of thing I usually just poke at my http headers until they look the same as the headers being sent form a web browser. Problem is knowing what the web browser and android app are sending so here's something to try.
This is not necessarily a code solution to your problem, but may be helpful.
Use Charles (great tool) http://www.charlesproxy.com/ for this. And no I don't have any connection to the company, it's just a great tool.
There's a feature in Charles called a reverse proxy and basically it allows you to bounce traffic through Charles to your drupal server and you can inspect it as it flows to and from your app.
Using charles you can sniff what a good request from your web browser looks like and then you can sniff what the requests from your android app look like. Compare the two and you can see where your app is badly shaping the request headers.
The debug phase goes like this:
Once you've got Charles set up, hit your drupal server a couple of times and inspect the structure of the request/response that you're seeing from the browser.
Then hit your service a couple of times from the android app and note the differences. Maybe the cookie isn't going through, maybe it's malformed, maybe there's something else about the headers. This will let you see what you need to shoot for in order to get Drupal to accept the requests.
来源:https://stackoverflow.com/questions/6166121/drupal-android-app-login-persistence