Android (Android) WebView settings cookie

Source: Internet
Author: User
Tags setcookie

The last two days have been trying to use the Android simulation landing, using the new WebView display can be accessed after landing pages, but no matter how to access the landing page to access, or jump back to the landing page, and then read the online cookie is not set, find a half-day to the appropriate setting method:


Login method:

Private Cookie cookie;public static HttpContext Localcontext;<span style= "Color:rgb (255, 0, 0); "><strong>public Static list<cookie> cookies;//This is a saved cookie</strong></span>public String cookiestr;//public static Cookie cookie = NULL;  /** * Save Cookie at login * @param URL * @param data * @return */public string Login (String url,string data[]) {HttpClient client = Null String html = null;try {client = new defaulthttpclient (); <span style= "Color:rgb (255, 0, 0);  "><strong>//creates a local instance of the cookie store Cookiestore cookiestore = new Basiccookiestore ();  Localcontext = new Basichttpcontext (); Bind custom COOKIE STORE to local content Localcontext.setattribute (Clientcontext.cookie_store, Cookiestore); </strong></span>httppost post = new HttpPost (URL); list<namevaluepair> parameters = new arraylist<namevaluepair> ();p Arameters.add (New Basicnamevaluepair (" Username ", data[0]));p Arameters.add (New Basicnamevaluepair (" passwd ", data[1]);p Arameters.add (New BasiCnamevaluepair ("Login", "%B5%C7%A1%A1%C2%BC")); urlencodedformentity entity = new Urlencodedformentity (Parameters, "Utf-8");p ost.setentity (entity); HttpResponse response = Client.execute (Post<span style= "Color:rgb" (255, 0, 0); ">,localContext</span>)" if (Response.getstatusline (). Getstatuscode () = =) {InputStream content = Response.getentity (). getcontent (); BufferedReader buffer = new BufferedReader (new InputStreamReader (content, "GBK")); String line = Null;while ((Line=buffer.readline ())!=null) {html+=line;} <span style= "Color:rgb (255, 0, 0); "><strong> cookies = cookiestore.getcookies ();</strong></span> System.out.println (" Cookies.size (): "+cookies.size ());  if (!cookies.isempty ()) {for (int i=0;i<cookies.size (); i++) {System.out.println ("-" +cookies.get (i). toString ());} }buffer.close ();} Else{system.out.println ("Utilslogin:" +response.getstatusline (). Getstatuscode ());}} catch (Exception e) {e.printstacktrace ();} Finally{if (client!=null) {Client.getconnecTionmanager (). shutdown ();}} return HTML;}

The OnCreate () method in activity:

Private WebView wvtempshow;private String receiveurl, @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_temp_web_view); wvTempShow = (WebView) Findviewbyid (r.id.wv_tempshow); Intent Intent = This.getintent (); receiveurl = Intent.getstringextra ("OpenUrl");< Span style= "Color:rgb (255, 0, 0); "><strong>//get cookie manager Cookiemanager Cookiemanager = cookiemanager.getinstance ();//Get the cookiestring when landing Oldcookie = UtilsLogin.cookies.get (0). GetName () + "=" +utilslogin.cookies.get (0). GetValue () + ";" +utilslogin.cookies.get (1). GetName () + "=" +utilslogin.cookies.get (1). GetValue () + ";";</strong></span> System.out.println ("Oldcookie:" +oldcookie); Cookiemanager.setcookie (Receiveurl, Oldcookie); Wvtempshow.getsettings (). Setdefaulttextencodingname ("GBK"); Wvtempshow.loadurl (Receiveurl); Wvtempshow.setwebviewclient (New Mywebviewclient ());} Class Mywebviewclient extends webviewclient{@Overridepublic void onpagestarted (WebvieW view, String URL, Bitmap favicon) {super.onpagestarted (view, URL, favicon); } @Overridepublic void onpagefinished (WebView view, String URL) {super.onpagefinished (view, URL);}}

Note: To see what kind of cookie string is required to access the page, you can use the following methods:

Cookiemanager Cookiemanager = Cookiemanager.getinstance (); String cookiestr = Cookiemanager.getcookie ("http://bkjw.guet.edu.cn/student/public/menu.asp?menu=mnall.asp");            System.out.println ("tempwebviewonpagefinished =" + cookiestr);


Then you spell it yourself like the above string Oldcookie, and then in Cookiemanager.setcookie (url,cookiestring), set



Android (Android) WebView settings cookie

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.