lundi 7 avril 2014

Android comment partager des Cookies entre DefaultHttpClient et le mode Web - Stack Overflow


how to share Cookies between DefaultHttpClient and WebView????


It took me one week to solve sharing Cookies between DefaultHttpClient and WebView, I searched lots of posts in stackoverflow, tried a few methods, but they did't work. At last, I found the method is great, so I want to share completely code in there, I hope this will help others.


LoginActivity.java, the core code is doPost


private int mNumber = 3;
public InputStream doPost(String url, HashMap<String, String> params,
String headParam, ArrayList<String> keyValues) {

DefaultHttpClient httpClient = null;
InputStream inputStream = null;
HttpResponse httpResponse = null;
int statusCode = -1;

httpClient = (DefaultHttpClient) NetworkManager.getHttpClient();
HttpPost httpPost = new HttpPost(url);

if (headParam != null) {
httpPost.addHeader("Cookie", headParam);
}

if (params != null) {

List<NameValuePair> httpRequestParams = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iter = params.entrySet().iterator();
while (iter.hasNext()) {

Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String val = entry.getValue();
if (val.equals("multi")) {

for (String values : keyValues)
httpRequestParams.add(new BasicNameValuePair(key,
values));
} else
httpRequestParams.add(new BasicNameValuePair(key, val));
}

try {
httpPost.setEntity(new UrlEncodedFormEntity(httpRequestParams,
HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

try {
httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
httpPost.abort();
} catch (IOException e) {
e.printStackTrace();
httpPost.abort();
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
return null;
}
}

if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
try {
inputStream = httpResponse.getEntity().getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
httpPost.abort();
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
} else {
}
}
} else {
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
}
}

Config.mCookies = httpClient.getCookieStore().getCookies(); //save cookies
return inputStream;
}

in Config.java


         public static List<Cookie> mCookies = null;

after login success, webview browse


List<Cookie> cookies = Config.mCookies;
if (cookies != null && !cookies.isEmpty()) {

CookieSyncManager.createInstance(mContext);
CookieManager cookieManager = CookieManager.getInstance();
for (Cookie cookie : cookies) {

Cookie sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "="
+ sessionInfo.getValue() + "; domain="
+ sessionInfo.getDomain();
cookieManager.setCookie("http://stackoverflow.com", cookieString);
CookieSyncManager.getInstance().sync();
}
}
mWebview.loadUrl(mLink);

setCookie(url, string); the "url" is host, at first I use domain "stackoverflow.com", it does not work, must be host "http://stackoverflow.com", this is very important.



how to share Cookies between DefaultHttpClient and WebView????


It took me one week to solve sharing Cookies between DefaultHttpClient and WebView, I searched lots of posts in stackoverflow, tried a few methods, but they did't work. At last, I found the method is great, so I want to share completely code in there, I hope this will help others.


LoginActivity.java, the core code is doPost


private int mNumber = 3;
public InputStream doPost(String url, HashMap<String, String> params,
String headParam, ArrayList<String> keyValues) {

DefaultHttpClient httpClient = null;
InputStream inputStream = null;
HttpResponse httpResponse = null;
int statusCode = -1;

httpClient = (DefaultHttpClient) NetworkManager.getHttpClient();
HttpPost httpPost = new HttpPost(url);

if (headParam != null) {
httpPost.addHeader("Cookie", headParam);
}

if (params != null) {

List<NameValuePair> httpRequestParams = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iter = params.entrySet().iterator();
while (iter.hasNext()) {

Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String val = entry.getValue();
if (val.equals("multi")) {

for (String values : keyValues)
httpRequestParams.add(new BasicNameValuePair(key,
values));
} else
httpRequestParams.add(new BasicNameValuePair(key, val));
}

try {
httpPost.setEntity(new UrlEncodedFormEntity(httpRequestParams,
HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

try {
httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
httpPost.abort();
} catch (IOException e) {
e.printStackTrace();
httpPost.abort();
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
return null;
}
}

if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
try {
inputStream = httpResponse.getEntity().getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
httpPost.abort();
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
} else {
}
}
} else {
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
}
}

Config.mCookies = httpClient.getCookieStore().getCookies(); //save cookies
return inputStream;
}

in Config.java


         public static List<Cookie> mCookies = null;

after login success, webview browse


List<Cookie> cookies = Config.mCookies;
if (cookies != null && !cookies.isEmpty()) {

CookieSyncManager.createInstance(mContext);
CookieManager cookieManager = CookieManager.getInstance();
for (Cookie cookie : cookies) {

Cookie sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "="
+ sessionInfo.getValue() + "; domain="
+ sessionInfo.getDomain();
cookieManager.setCookie("http://stackoverflow.com", cookieString);
CookieSyncManager.getInstance().sync();
}
}
mWebview.loadUrl(mLink);

setCookie(url, string); the "url" is host, at first I use domain "stackoverflow.com", it does not work, must be host "http://stackoverflow.com", this is very important.


0 commentaires:

Enregistrer un commentaire