Android-HttpURLConnection自己主动管理cookie
2024-08-30 09:36:09
Volley那么好用的框架居然没有内置对cookie的处理,自己搞一个!
public class MobCookieManager {//转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/46890245 private MobCookieManager(){} /**
* 应用启动的时候调用,參考:{@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";
public static final String FORMAT_YMD = "yyyy-MM-dd";
public static final String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_GMT = "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'"; private 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<String, SimpleDateFormat> initialValue() {
Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>();
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, SimpleDateFormat> 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);
} }
注意:我们这里使用的android.webkit.CookieManager。
最新文章
- VC++常用数据类型转化
- 思科ACI是一种什么样的技术?
- JavaWeb学习笔记——XML和SAX解析区别
- erlang mnesia 数据库实现SQL查询
- h2database源码浅析:MVTable与MVIndex
- UIImageC处理
- [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)
- 分享到QQ空间、新浪微博、腾讯微博的代码!(收藏)
- MVC 中引入Jquery文件的几种方法
- RBF径向基神经网络——乳腺癌医学诊断建模
- jsp页面适应手机页面
- asp.net mvc3 数据验证(三)—自定义数据注解
- P8 Visible Lattice Points
- svn 安装网站
- Multi-View 3D Reconstruction with Geometry and Shading——Part-2
- bzoj4514 [Sdoi2016]数字配对(网络流)
- Selenium WebDriver使用IE浏览器 属性设置
- jQuery如何取得HiddenField值(转)
- leetcode412
- ubuntu16.04 nginx安装
热门文章
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
- 【DataStructure】The difference among methods addAll(),retainAll() and removeAll()
- poj--3207--Ikki&#39;s Story IV - Panda&#39;s Trick(2-sat)
- System.setProperty 与 System.getProperty
- [jzoj 6093] [GDOI2019模拟2019.3.30] 星辰大海 解题报告 (半平面交)
- 7.treeview
- C#比较二个数组并找出相同或不同元素的方法
- RTSP/RTP 媒体传输和控制协议
- Cookie是存储在客户端上的一小段数据
- vue中slot组件的使用