In my android app i want to maintain the session id for at least 30 days even if the app quits. I am using DefaultHttpClient. When i send first request to the server it returns session id, i have to use this returned session id for all other request.
You can set a Shared Preference to maintain your session ID. You can use a second pref to store the date of the last connection so you are able to reset the session ID after 30 days of inactivity.
Right U can save your Session ID in SharedPref, However there are other value that need to be saved , if your cookies expires u can relogin again with same credential again , other wise u can use persistence cookies (becoz user can kill the apps anytime ), below code snippets is only for till user used app (user does kill the app process )
private static CookieStore cookieStore = new BasicCookieStore();
InputStream is = null;
try {
final DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
Log.d("vipin", url.toString());
HttpPost httpPost = new HttpPost(url);
/** maintain session if logged in */
if (SouqApplication.getBooleanValue(Constants.IS_LOGGED_IN))
httpPost.setHeader(Constants.PHPSESSID, SouqApplication.getStringValue(Constants.SESSION_ID));
//httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpClient.setCookieStore(cookieStore);
HttpResponse httpResponse = httpClient.execute(httpPost, localContext);
setCookies(httpClient.getCookieStore().getCookies());
HttpEntity httpEntity = httpResponse.getEntity();
String errorHandlling = EntityUtils.toString(httpEntity);
return errorHandlling ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
And for a Better approach U can use a ready cake popular library
Persistent Cookie Storage with PersistentCookieStore
http://loopj.com/android-async-http/
Related
I'm coding a login system which will keep the user permanently logged on (until username or password is incorrect) but I'm having an issue with cookie storage. What I want to do is have the cookies store in local storage (probably shared preference). Though I have no idea where to start. This is my main HTTP Post function.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Add your data
httppost.setHeader("X-Requested-With", "XMLHttpRequest");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
StringBuffer sb = new StringBuffer();
BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
return sb.toString();
} catch (Exception e) {
//TODO: WIP
e.printStackTrace();
}
I want to set the cookies first (of course if there are any) then I would like to resave them after the httppost has executed. Where can I go about doing this?
Edit there is about 4 cookies that are saved.
This should help you get started:
http://docs.oracle.com/javase/tutorial/networking/cookies/definition.html
Check out this post as well:
https://stackoverflow.com/a/3587332/2495131
The background of my service: it implements LocationListener and in LocationManager instance (locMananager) registers for updates:
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
In onLocationChanged method it calls a method named recordTrace with a current location, which then calls getHttpResponse to send the location coordinates to a server. The latter method is as follows:
public InputStream getHttpResponse(String url, ArrayList<NameValuePair> params, int timeout) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
httpClient.setParams(httpParams);
HttpPost post = new HttpPost(url);
if(params != null && !params.isEmpty()) {
try {
post.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "HttpPost.setEntity Error: " + e.getMessage());
lastError = "błąd HTTP";
}
}
CookieStore store = new BasicCookieStore();
if(localCookies != null && localCookies.size() > 0) {
for(int i = 0; i < localCookies.size(); i++) {
store.addCookie(localCookies.get(i));
}
}
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.COOKIE_STORE, store);
HttpResponse response = null;
HttpEntity entity = null;
InputStream content = null;
try {
response = httpClient.execute(post, context);
store = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
List<Cookie> cookies = store.getCookies();
if(cookies != null && cookies.size() > 0) {
localCookies = new ArrayList<BasicClientCookie>();
for(Cookie cookie : cookies) {
localCookies.add((BasicClientCookie) cookie);
}
}
entity = response.getEntity();
content = entity.getContent();
return content;
} catch (ClientProtocolException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
}
}
params is a NameValuePair with prepared data, timeout is set to 5000 (5 seconds). ArrayList localCookies holds cookies saved before, after successful logging in (to keep the session).
The problem is: when I loose a mobile signal (i.e. when I go to a subway) and restore it, I get IOException, which is unrecoverable unless I restart the phone.
Any help would be appreciated. I'm loosing my mind and going bald!
Thanks,
Peter.
EDIT
I've done some research. After the method getHttpResponse is invoked, I utilize InputStream returned by it, but don't close it after all. Do you thing that might be the issue? Is this possible that the operator breaks the connection and then establishes a new one, whereas my InputStream somehow "keeps" the former connection and produces the problems with data transfer?
I added a finally block where now the InputSTream is closed. However, since it's hard to cause the problem on demand (it doesn't happen regularly), I can't check if closing stream solves it.
After a few days of testing it seems I've found the solution. Calling 2 methods solves the issue:
close the InputStream returned by httpResponse.getEntity()
shutdown the connection by executing httpClient.getConnectionManager().shutdown()
Code snippet of a complete request and response handling:
String url = "www.someurl.com";
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("login", "mylogin"));
params.add(new BasicNameValuePair("password", "mypassword"));
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream content = httpEntity.getContent();
/*
* utilize content here...
*/
content.close(); // here's the point
httpClient.getConnectionManager().shutdown(); // the second important thing
}
catch (UnsupportedEncodingException e) {}
catch (ClientProtocolException e) {}
catch (IOException e) {}
I'm answering my own question since I've spent a lot of time on searching what's causing the problem and I think I can save somebody's time. After a few days the aplication is still working and doesn't break the connection.
Regards,
Peter.
I was able log in through my android app to http://yearbook08.com/ using this code:
String URL="http://yearbook08.com/login.php";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = null;
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userId", uname));
nameValuePairs.add(new BasicNameValuePair("password", pass));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now when I move to another activity I want to retrieve http://yearbook08.com/wall.php but the web server does not recognize my last login and asks me to login again.
Is there a way that I can stay logged in after once logging in? Kindly help !
Your code does not take into account session management. This happening as after a successful login no cookies are set, so if you send another request the initial authentication is lost and server takes it to be a fresh request.
I would suggest you use Apache httpcomponents library 4.x (httpclient in particular)
Create a httpcontext and attach a cookie store as
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://www.google.com/");
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
Make use the same localContext in the subsequent requests. Also note cookiestore and httpcontext should be declared as static or global variables as their scope must exist wherever you carry the httprequest. You don't have to set cookies on your own, it is automatically done!
And do read about HttpComponents 4.x http://hc.apache.org/httpcomponents-client-ga/index.html
Update 1:
Make sure it is v4 and v3. basicCookiestore has only been present since v4. It is clear now that you are using v3 which does not know about basiccookiestore object. Add the v4 library to your project. It will solve your problem
Update 2
If you try to retrieve http://yearbook08.com/wall.php from the 2nd activity this problem will arise as the 2nd activity won't have httpcontext,httpclient or the Cookiestore object in it. So it will send a fresh request.
1st approach
So you should try to fetch the contents you need in the first activity itself and then pass on the fetched data to the 2nd activity. This way you'll have session maintained.
2nd soln
If you are not satisfied with this you can check this out http://www.jameselsey.co.uk/blogs/techblog/android-implementing-global-state-share-data-between-activities-and-across-your-application/ Share httpcontext, cookiestore and httpclient across the activities to accomplish the task.
I am trying to maintain a logged-in user session between my Android app and my Drupal website. In my research, it comes down to sending cookie(s) back to Drupal but I am struggling to implement it. How can I make a start on this?
Just in case anyone else got the same issue, I had similar problem and I was able to solve it by the following code:
1- Define CookieManager and CookieStore in your class
CookieManager cookieManager;
CookieStore cookieStore;
2- Add default cookie handler, e.g. in the class constructor or in OnCreate method
cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
3- Use the cookie storage when you do HTTP request
public byte[] openURI(String uri) {
try {
URI uriObj = new URI(uri);
DefaultHttpClient client = new DefaultHttpClient();
// Use the cookieStor with the request
if (cookieStore == null) {
cookieStore = client.getCookieStore();
} else {
client.setCookieStore(cookieStore);
}
HttpGet getRequest = new HttpGet(uriObj);
HttpResponse response = client.execute(getRequest);
// Read the response data
InputStream instream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength();
byte[] data = new byte[contentLength];
instream.read(data);
response.getEntity().consumeContent();
return data ;
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I am pretty sure that if you use the HttpClient provided with the Android APIs it should do session management with cookies for you until you close the connection manually.
If I am wrong on this, then you can easily work around this by implementing your own cookie store using the CookieStore interface or the BasicCookieStore class. If all else fails, you can store cookies manually and set cookies in the header each time you make a HTTP request.
I am not sure how this might change for your particular problem though but this should most likely work considering the description of the problem you gave.
I'm building an android app which should perform a GET on my site to get two cookies and then perform a post to the same site with these cookies.
As mentioned I start of with the GET and I'm using org.apache.http.client.HttpClient to perform this operation.
String requiredCookies = "";
HttpContext localContext = null;
System.out.println("------------------GET----------------------");
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("www.mysitegeturl.com");
//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();
// Creating a local HTTP context
localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);
HttpResponse response;
try {
response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
//Do this so that Java.net impl should work
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
requiredCookies += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}
if (entity != null) {
entity.consumeContent();
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("------------------GET-END---------------------");
So far so good. Don't mind the requiredCookies line yet, it will be used in the Java.net impl since I can't get the HttpClient one to work =(.
Let's take a look at the non working HttpClient Post part.
System.out.println("------------------HttpClient - POST----------------------");
HttpPost post = new HttpPost("www.mysiteposturl.com");
//Params
HttpParams params = new BasicHttpParams();
params.setParameter("foo", "post");
params.setParameter("bar", "90");
params.setParameter("action", "search");
post.setParams(params);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
HttpResponse response2 = httpClient.execute(post, localContext);
System.out.println(response2.getStatusLine());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------------------POST END---------------------");
What happens now is that I perform a POST with the localContext where the cookies are stored. This doesn't work. I get a HTTP/1.1 401 No session. Since I had no luck with this I tried another approach(java.net.HttpURLConnection). Remember I still use the same GET part
URL url = new URL("www.mysiteposturl");
HttpURLConnection connection = null;
String dataString = "bar=90&foo=post&action=search";
try {
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie", requiredCookies);
//Set to POST
connection.setDoOutput(true);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(dataString);
writer.flush();
writer.close();
connection.connect();
if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
System.out.println(connection.getContent().toString());
} else {
System.out.println("Error");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------------------POST END---------------------");
And VIOLA a 200 is displayed and everything works like a charm. What do you guys think? Could someone please provide me with an answer because I can't figure it out.
The problem appears to be that you have two different host names in the setup. This will cause HTTP Client to not send cookies for a different host. You could try changing the domain of the cookies in the cookie store, or using the same host for GET and POST. Additionally you could manually add the cookies to the headers in HTTP Client as you did in the HttpURLConnection example.
I guess it was a mistake that you used two completely different domains for your two requests — i.e. you were trying to mask your real URL? If not, then that's why you're not getting any cookies. If were just trying to mask your URL, well that's why example.com exists. :)
Alternatively, and this is completely off the top of my head from code I wrote last week — it worked fine across multiple GETs, POSTs and subdomains:
DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore cookieJar = new BasicCookieStore();
httpClient.setCookieStore(cookieJar);
i.e. I'm explicitly using a DefaultHttpClient, which I believe has those extra get/setters for the cookie store. I don't think I used any context objects either.