Android WebView Save Cookie Login

Source: Internet
Author: User

Because of the project needs, you need to embed the Web page in the app, log in using Nativie, and then save the cookie to WebView for free login. The method of synchronizing cookies to WebView has a large number of references on the web, and you can refer to the following code:

/*** sync cookie*/private void synccookie (Context context, string url) {         try{             LOG.D ("Nat: webview.synccookie.url",  url);                         Cookiesyncmanager.createinstance (context);             cookiemanager cookiemanager = cookiemanager.getinstance ();             cookiemanager.setacceptcookie (True);             cookiemanager.removesessioncookie ();//  removal              cookiemanager.removeallcookie ();           &nbSp; string oldcookie = cookiemanager.getcookie (URL);             if (oldcookie != null) {                 LOG.D ("Nat: webview.synccookieoutter.oldcookie",  oldcookie);            }             StringBuilder sbCookie = new  StringBuilder ();             sbcookie.append ( String.Format ("jsessionid=%s", "input your jsessionid string"));             sbcookie.append (String.Format (";d omain=%s",  "INPUT YOUR  domain string "));             Sbcookie.append (String.Format (";p ath=%s", "INPUt your path string "));             String cookievalue = sbcookie.tostring ();             cookiemanager.setcookie (Url, cookievalue);             cookiesyncmanager.getinstance (). Sync ();              string newcookie = cookiemanager.getcookie (URL);             if (newcookie != null) {                 LOG.D ("Nat:  WebView.syncCookie.newCookie ",  newcookie);             }        }catch (exception e) {              LOG.E ("Nat: webview.synccookie failed",  e.tostring ());         }    }


Use the above method to sync cookies to webview so that you can browse the Web without logging in.

But in the actual use of the process found that the cookie is not saved successfully, each time will jump to the login page, tangled for a long time, and finally found that it is in the initialization of webview missing something important. You can refer to the following my Code settings webview.

/*** init webview settings* */    private void  Initwebviewsettings () {//        mywebview.getsettings (). SetSupportZoom (true);//        mywebview.getsettings (). Setbuiltinzoomcontrols (True) ;//        mywebview.getsettings (). Setdefaultfontsize (;// )        mywebview.getsettings (). Setloadwithoverviewmode (True);         //  settings can access files          Mywebview.getsettings (). Setallowfileaccess (true);         // If you have JavaScript on the page you are visiting, WebView must set up support javascript         Mywebview.getsettings (). Setjavascriptenabled (True);         Mywebview.getsettings (). Setuseragentstring (Myapplication.getuseragent ());  &Nbsp;      mywebview.getsettings (). Setcachemode (Websettings.load_no_cache);         mywebview.getsettings (). Setallowfileaccess (True);         mywebview.getsettings (). Setappcacheenabled (True);         mywebview.getsettings (). Setdomstorageenabled (True);         mywebview.getsettings (). Setdatabaseenabled (true);     }


Complete the above two steps, run the program again, you will find that the Web page will not be redirected to the login page.

The first use of the WebView control, originally thought very simple, but accidentally fell out of the pit, everyone careful.





Android WebView Save Cookie Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.