How HTTP loading in Android gets cookies and WebView loading pages how to get cookies

Source: Internet
Author: User

Recently done projects on the phone side login HTTP request and WebView record login to get cookie information, can view cookie information.

HTTP request for cookie information:

   Public Staticstring Request (String Httpurl, string params, context context) {BufferedReader Reader=NULL; String result=NULL; String Httpurl= "Http://xwwscs.com"; StringBuffer SBF=NewStringBuffer (); Try{URL URL=NewURL (Httpurl + "/app.php")); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setrequestmethod ("POST"); Connection.setrequestproperty ("Cookie", Cookieutil.getparam (context). ToString ()); Connection.setconnecttimeout (3000); //whether to enter parametersConnection.setdooutput (true); byte[] Bypes =params.tostring (). GetBytes (); Connection.getoutputstream (). write (bypes);//Input ParametersConnection.connect (); InputStream is=Connection.getinputstream (); Reader=NewBufferedReader (NewInputStreamReader (IS, "UTF-8")); String Strread=NULL;  while((Strread = Reader.readline ())! =NULL) {sbf.append (strread); Sbf.append ("\ r \ n");            } reader.close (); Map<string, list<string>> cookie_map =Connection.getheaderfields (); List<String> cookies = cookie_map.get ("Set-cookie"); if(NULL! = Cookies && 0 <cookies.size ()) {String s= "";  for(String cookie:cookies) {if(S.isempty ()) {s=cookies; } Else{s+= ";" +cookies; }} log.i ("Cookie", s); } result=sbf.tostring (); } Catch(Exception e) {result= "Error";        E.printstacktrace (); }        returnresult; }}

WebView loading web page for cookie

Webview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated Method Stub                return Super. shouldoverrideurlloading (view, URL); } @Override Public voidonpagefinished (WebView view, String URL) {Cookiemanager cookiemanager=cookiemanager.getinstance (); String cookiestr=cookiemanager.getcookie (URL); if(cookiestr!=NULL) {log.i ("Cookie", cookiestr); }                Super. onpagefinished (view, URL);    }                      }); 

How HTTP loading in Android gets cookies and WebView loading pages how to get cookies

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.