I have an DefaultHttpClient that don’t check for certificates that I use at both activity:
public function clientWithoutCertificateCheck() {
DefaultHttpClient httpClient = new DefaultHttpClient();
try{
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
httpClient = new DefaultHttpClient(ccm, params);
} catch (Exception e) { }
return httpClient;
}
and it does login at a page inside this same activity.
After start the other activity I save it’s cookies:
CookieSyncManager.createInstance(this);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for(Cookie cookie : cookies)
{
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);
}
Then I’m trying to get the same httpClient at another activity.
DefaultHttpClient httpClient = clientWithoutCertificateCheck();
String url = "https://academicos.unilasalle.edu.br/";
String[] keyValueSets = CookieManager.getInstance().getCookie(url).split(";");
for(String cookie : keyValueSets)
{
String[] keyValue = cookie.split("=");
String key = keyValue[0];
String value = "";
if(keyValue.length>1) value = keyValue[1];
httpClient.getCookieStore().addCookie(new BasicClientCookie(key, value));
}
and for some reason he isn’t logged in anymore.
Can someone help me solve this?
According to your code, obviously by calling clientWithoutCertificateCheck() always return a newly created httpClient, not the same one from previous activity. So on the server side, it may think these are different incoming connection and associate with different sessions/cookies.
Activity’s life cycle is quite transitory, usually it gets created/destroyed many times during application running time. From my point of view, it is not reasonable to bind httpClient to Activity.
If you want to retain/use single httpClient instance cross avtivities, consider using service, implement/centralize your http related stuff into your HttpService, and start/bind service for all activities that need use http service. hope that help.