I can't read all my cookies, in my webview i can only read "PHP_AUTH_SID" named cookie, but in browser i can see all cookies. Here is my code:
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(this);
webView = (WebView) findViewById(R.id.web_login);
And this is my shouldOverrideUrlLoading() method in web view client
CookieSyncManager.getInstance().sync();
final CookieManager cookiesManager = CookieManager.getInstance();
cookiesManager.setAcceptCookie(true);
final String cookies = cookiesManager.getCookie(COOKIES_HOST);
Log.i("123123", cookies);
in LogCat i can see only: "09-13 14:05:48.139: I/123123(21188):PHP_AUTH_SID=..."
in browser:
PHP_AUTH_SID=...,
access_token=...
It seems that the output from LogCat is just cut off. Just try to debug and see the "really" content of "cookies" string.
I think you could just parse the cookies string with String[] elements = cookies.split("=")
Related
Needed datas is not written to cookies immediately in WebView. But when I wait for 10-15 seconds everything is ok.
To explain the situation, this example would be good as for me:
I open the app and login. After login, I close the app immediately. Then after I open the app again, it shows me to logout. But if I open the app after 1 minute, it shows again as logged in. For me cookies are written lately. But I cannot find solution. Please help me if you know.
I used CookieManager class but it doesn't help either.
CookieManager.getInstance().setAcceptCookie(true);
I had similar issue and I added the below code and worked.
String myURL = "https://www.yourWebPage.com";
android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
cookieManager.setAcceptFileSchemeCookies(true);
cookieManager.getInstance().setAcceptCookie(true);
cookieManager.getCookie(myURL);
Hope it helps.
I had the similar problem, what I did is to get the cookies when login and set cookies for the load url you set on webView
#Nullable
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String resourceUrl = request.getUrl().toString();
Log.e(MainActivity.TAG, "the request url :" + resourceUrl);
CookieManager cookieManager1 = CookieManager.getInstance();
// get the resourceUrl that has session
if (resourceUrl.equals("the url has session")){
String Cookies = cookieManager1.getCookie(resourceUrl);
if (Cookies != null && Cookies.contains("sessionid")){
String[] cookiesList = Cookies.split(";");
cookieManager1.removeSessionCookies(null);
for (String c : cookiesList) {
cookieManager1.setCookie("the load url", c);
}
}
}
return super.shouldInterceptRequest(view, request);
}
I have en app with a webview to display an e-shop website.
For various reason the login par is made in a native form with a DefaultHttpClient and HttpPost.
When the user start by login, i have no problème to save the session cookie from the request to the webview cookie store.
My problem is when the user first navigate through the webview an login after, i cant retrive the webview cookie session from the cookiestore to put it to my DefaultHttpClient
this is my code :
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
CookieSyncManager.getInstance().sync();
cookieManager.setAcceptCookie(true);
DefaultHttpClient client = new DefaultHttpClient(clientConnectionManager, params);
String[] keyValueSets = cookieManager.getCookie(context.getString(R.string.cookie_domaine)).split(";");
for(String cookie : keyValueSets)
{
String[] keyValue = cookie.split("=");
String key = keyValue[0];
String value = "";
if(keyValue.length>1) value = keyValue[1];
BasicClientCookie2 cookieForRequest = (BasicClientCookie2) new BasicClientCookie2(key, value);
cookieForRequest.setDomain(context.getString(R.string.cookie_domaine));
cookieForRequest.setPath("/");
client.getCookieStore().addCookie(cookieForRequest);
}
In debug i see my session cookie but the getCookie method dont return it, i think it's because the cookie session domain start with a dot.
How can i maintain my webview session to a DefaultHttpClient request?
I have some http requests. One of them retrieves and parse cookie from it's response. I save this cookie via CookieSyncManager and CookieManager with following code:
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
String cookieString = cookie.getName() + "=" + cookie.getValue();
Log.e(getClass().toString(), cookieString);
cookieManager.setCookie(START_PAYMENT_URL, cookieString);
CookieSyncManager.getInstance().sync();
Log.e(getClass().toString(), "Get cookie: " + cookieManager.getCookie(START_PAYMENT_URL));
Both Log.e calls write same cookie. So everything looks ok.
I have different activity which contains WebView. I need to call postUrl(String url) method with some POST params and with authorization cookie. I thought that cookie is in CookieManager and everything should work great. Well. It is. But on 4.x devices only.
On 2.x devices WebView makes postUrl without cookie.
Here is activity code which contains WebView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.payment_webview);
final String billId = getIntent().getStringExtra(INTENT_BILL_ID);
final WebView webView = (WebView) findViewById(R.id.payment_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(getClass().toString(), url);
view.loadUrl(url);
return false;
}
});
String postData = "id_bill=" + billId;
Log.d(TAG, "Requesting payment URL " + START_PAYMENT_URL + " with post data: " + postData);
Log.d(TAG, CookieManager.getInstance().getCookie(START_PAYMENT_URL));
webView.postUrl(START_PAYMENT_URL, EncodingUtils.getBytes(postData, "BASE64"));
}
CookieManager.getInstance().getCookie(START_PAYMENT_URL) returns null on 2.x devices and cookie value on 4.x devices.
How to fix this problem?
Seems like I figured out what's the problem.
I just removed
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
while I'm saving cookies.
I suppose that problem is that all methods of CookieManager work asynchronously. Probably remove cookies methods invoked after cookieManager.setCookie(START_PAYMENT_URL, cookieString); even if they called before it. So CookieManager saves cookies and after that remove cookies methods invoked.
Hello Stackoverflowers!
I have written a relatively simple application that consists of a login text field, a password text field and a login button.
My goal is that when the user enters the login information and touches the login button, the application will log the user into the website I have specified and open it up in a different intent or WebView. My current implementation opens a new activity with a WebView and passes in the login information. My code is as follows for the new activity:
setContentView(R.layout.web);
try {
//add the users login information(username/pw) to a List
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "random#gmail.com"));
nameValuePairs.add(new BasicNameValuePair("password", "password1"));
//declare a new HttpClient
HttpClient httpclient = new DefaultHttpClient();
//set the HttpPost to the desire URL
HttpPost httppost = new HttpPost(URL_STRING);
//set the entity to a new UrlEncodedForm with the login info and type
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
//store the response
HttpResponse response = httpclient.execute(httppost);
//get the data from the response
String data = new BasicResponseHandler().handleResponse(response);
//get the webview from the xml
WebView webview = (WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
//load the return website from the server
webview.loadDataWithBaseURL(httppost.getURI().toString(), data, "text/html", HTTP.UTF_8, null);
This successfully logs me in to the URL (an https site) and opens the page, however if you try to click on any of the buttons on the website it takes you back to the login page in the WebView, and it does not display many of the attributes on the website (charts/graphs).
Could this be a cookie thing?
So is there a way to send the login info via a new intent? Or is there a solution to my WebView implementation?
(here is a relatively similar(ish) question that never got a definitive answer Android SSO (Single sign-on) for app)
Thank you for your time! I really appreciate you taking a look at my question.
EDIT: So giseks's solution worked for me to get the webpage to stay within the WebView, however the charts/graphs/etc on the page still did not display, the solution for that was as simple as enabling javascript for the WebSettings
webview.getSettings().setJavaScriptEnabled(true);
Here's Google's WebSettings Android API for reference: WebSettings
This helped me, I hope it helps you!
My guess is that your application doesn't handle cookies right. Take a look at this question, it may help.
WebView and Cookies on Android
EDIT
In your code you seem to pass only the html retrieved from request to the WebView. Cookies seem to get lost somewhere. I'd suggest you another approach.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webv = (WebView)findViewById(R.id.MainActivity_webview);
webv.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
String postData = FIELD_NAME_LOGIN + "=" + LOGIN +
"&" + FIELD_NAME_PASSWD + "=" + PASSWD;
// this line logs you in and you stay logged in
// I suppose it works this way because in this case WebView handles cookies itself
webv.postUrl(URL, EncodingUtils.getBytes(postData, "utf-8"));
}
I want to logout my twitter account by deleting the cookies created by it. I am able to retrive the cookies created by twitter using code:
String twit_cookie = getCookie ("http://www.twitter.com");
But how can i delete only cookies created by twitter because removeAllCookie() deletes all the cookies created by browser. How can i delete the specific cookie by URL or by name???
Please help...
CookieManager class has a method setCookie. Have you tried it like:
setCookie("http://www.twitter.com", null);
Or perhaps
setCookie("http://www.twitter.com", "auth_token=''");
You can use the method CookieManager#setCookie(String url, String value). As stated in the docs:
Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie.
The "clearest" way is to set all cookies created by twitter to expired (a time in the past). The code from this answer is almost right, except the date is in the future.
Modified code:
final String domain = "http://www.twitter.com";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
String cookiestring = cookieManager.getCookie(domain); //get all cookies
String[] cookies = cookiestring.split(";");
for (int i=0; i<cookies.length; i++) {
String[] cookieparts = cookies[i].split("="); //split cookie into name and value etc.
// set cookie to an expired date
cookieManager.setCookie(domain, cookieparts[0].trim()+"=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
}
CookieSyncManager.getInstance().sync(); //sync the new cookies just to be sure