Android-httpurlconnection automatically Manage cookies

Source: Internet
Author: User
Tags setcookie

Volley so easy to use the framework does not have a built-in cookie processing, own one!
public class Mobcookiemanager {//reprint please indicate source: http://blog.csdn.net/goldenfish1919/article/details/46890245private Mobcookiemanager () {}/** * call when application starts, reference: {@link cookiemanager#getinstance cookiemanager.getinstance ()} * */public static void init (context context) {cookiesyncmanager.createinstance (context);} public static string GetCookie (string url) {Cookiemanager Cookiemanager = cookiemanager.getinstance (); return Cookiemanager.getcookie (URL);} /** * HTTP://STACKOVERFLOW.COM/QUESTIONS/16007084/DOES-ANDROID-WEBKIT-COOKIEMANAGER-WORKS-ON-ANDROID-2-3-6 * * * * public static void Setcookies (String url, map<string, list<string>> headerfields) {if (null = = Headerfields) { return;} list<string> cookies = headerfields.get ("Set-cookie"); if (null = = cookies) {return;} Cookiesyncmanager.getinstance (). Startsync (); for (String cookie:cookies) {setcookie (URL, cookie);} Cookiesyncmanager.getinstance (). sync ();} private static void Setcookie (string url, string cookie) {Cookiemanager Cookiemanager = cookieManager.getinstance (); Cookiemanager.setacceptcookie (true); if (Cookie.indexof ("Expires") < 0) {cookie = Addexpiretocookie (cookie);} Cookiemanager.setcookie (URL, cookie);} /** * Http://stackoverflow.com/questions/8547620/what-is-a-session-cookie * */private static String Addexpiretocookie (String cookie) {Date expiredate = new Date (new Date (). GetTime () + 24l*60*60*1000); String datestr =dateutil.format (DATEUTIL.EAST8TOGMT (expiredate), DATEUTIL.FORMAT_GMT); String arr[] = Cookie.split (";"); StringBuilder sb = new StringBuilder (); Sb.append (arr[0]); Sb.append (";"). Append ("expires="). Append (Datestr), if (Arr.length > 1) {for (int i=1; i<arr.length; i++) {sb.append (";"). Append (Arr[i]);}} return sb.tostring ();}}
</pre><pre name= "code" class= "java" ><pre name= "code" class= "Java" >public class Dateutil {public Static final String format_mdhm = "Mm-dd hh:mm";p ublic static final String format_ymd = "Yyyy-mm-dd";p ublic static final S Tring FORMAT_YMDHM = "Yyyy-mm-dd hh:mm";p ublic static final String format_ymdhms = "Yyyy-mm-dd HH:mm:ss";p ublic static fin Al String format_gmt = "EEE, dd-mmm-yyyy HH:mm:ss ' GMT '";p rivate static final String TAG = DateUtil.class.getSimpleName (); private static final Locale Default_locale = locale.china;private static threadlocal<map<string, SimpleDateFormat >> threadLocal = new threadlocal<map<string, simpledateformat>> () {protected synchronized map& Lt String, simpledateformat> InitialValue () {map<string, simpledateformat> Map = new hashmap<string, Si        Mpledateformat> ();        Map.put (FORMAT_MDHM, New SimpleDateFormat (FORMAT_MDHM, Default_locale)); Map.put (Format_ymd, New SimpleDateFormat (FORMAT_YMD, Default_locale));        Map.put (FORMAT_YMDHM, New SimpleDateFormat (FORMAT_YMDHM, Default_locale));        Map.put (Format_ymdhms, New SimpleDateFormat (Format_ymdhms, Default_locale));            Map.put (FORMAT_GMT, New SimpleDateFormat (FORMAT_GMT, Default_locale));          return map;        }      }; Private Dateutil () {} public static SimpleDateFormat GetDateFormat (String format) {map<string, Simpledatefo      rmat> map = (map<string, simpledateformat>) threadlocal.get ();    SimpleDateFormat SDF = map.get (format);    if (SDF! = null) {return SDF; }TRY{SDF = new SimpleDateFormat (format, Default_locale); map.put (format, SDF);} catch (Exception e) {mylog.e (TAG, e);}    return SDF;     public static Date Parse (string textDate, string format) {if (textDate = = NULL | | textdate.length () <= 0) {    return null;    } try{SimpleDateFormat sdf = getdateformat (format);    if (SDF = = null) {return null;   } return Sdf.parse (TextDate);   }catch (Exception e) {mylog.e (TAG, E);    return null; }} public static string format (date date, string format) {if (date = = null) {return null;} SimpleDateFormat SDF = getdateformat (format), if (SDF = = null) {return null;} return Sdf.format (date);} public static date east8togmt (date src) {if (src = = null) {return null;}      TimeZone Srctimezone = Timezone.gettimezone ("gmt+8");  TimeZone Desttimezone = Timezone.gettimezone ("GMT");          Long targettime = Src.gettime ()-srctimezone.getrawoffset () + Desttimezone.getrawoffset (); return new Date (targettime);}}

Note: We use the android.webkit.CookieManager here.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-httpurlconnection automatically Manage 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.