コード例 #1
0
 public void processResponseCookie(HttpURLConnection connection) {
   // -----------------------------------------------------------------------------------------------------
   // 1、获取响应的Set-Cookie值,得到响应的Cookie字符串集合
   // -----------------------------------------------------------------------------------------------------
   Map<String, List<String>> headerFields = connection.getHeaderFields();
   List<String> setCookies = headerFields.get("Set-Cookie");
   List<String> responseCookies = new ArrayList<String>();
   if (null != setCookies && setCookies.size() > 0) {
     for (String setCookie : setCookies) {
       if (setCookie.contains(",")) {
         String[] arrResponseCookies = setCookie.split(",");
         for (String rCookie : arrResponseCookies) {
           responseCookies.add(rCookie);
         }
       } else {
         responseCookies.add(setCookie);
       }
     }
   } else { // 服务端没有响应数据过来,直接不处理了
     return;
   }
   // -----------------------------------------------------------------------------------------------------
   // 2、获取CookieOrigin
   // -----------------------------------------------------------------------------------------------------
   CookieOrigin cookieOrigin = getCookieOrigin(connection);
   // -----------------------------------------------------------------------------------------------------
   // 3、根据响应的Cookie字符串集合与CookieOrigin,获取Cookie集合
   // -----------------------------------------------------------------------------------------------------
   CookieSpec cookieSpec = obtainCookieSpec();
   try {
     List<Cookie> cookies = cookieSpec.parse(responseCookies, cookieOrigin);
     // -----------------------------------------------------------------------------------------------------
     // 4、将验证服务端返回的Cookie,并存起来
     // -----------------------------------------------------------------------------------------------------
     for (Cookie cookie : cookies) {
       try {
         cookieSpec.validate(cookie, cookieOrigin); // 验证Cookie,不符合规则的将抛MalformedCookieException
         CookieStoreHandler.getInstance().addCookie(cookie);
       } catch (MalformedCookieException ex) {
         L.w("Cookie rejected [" + formatCooke(cookie) + "] " + ex.getMessage());
       }
     }
   } catch (MalformedCookieException ex) {
     L.w("Invalid Cookie Header [" + responseCookies + "] " + ex.getMessage());
   }
 }
コード例 #2
0
 public void processRequestCookie(
     HttpURLConnection connection, HashMap<String, String> cookieHeaders) {
   connection.setInstanceFollowRedirects(false);
   // -----------------------------------------------------------------------------------------------------
   // 1、获取所有的Cookie集合
   // -----------------------------------------------------------------------------------------------------
   CookieStore cookieStore = CookieStoreHandler.getInstance();
   List<Cookie> cookies = cookieStore.getCookies();
   if (null == cookies || cookies.size() <= 0) {
     return;
   }
   // -----------------------------------------------------------------------------------------------------
   // 2、获取CookieOrigin
   // -----------------------------------------------------------------------------------------------------
   CookieOrigin cookieOrigin = getCookieOrigin(connection);
   // -----------------------------------------------------------------------------------------------------
   // 3、过滤符合条件数据,获取符合的Cookie集合
   // -----------------------------------------------------------------------------------------------------
   CookieSpec cookieSpec = obtainCookieSpec();
   final List<Cookie> matchedCookies = new ArrayList<Cookie>();
   final Date now = new Date();
   boolean expired = false;
   for (final Cookie cookie : cookies) {
     if (cookie.isExpired(now)) { // 过期了..,没有设置expire不过期
       VolleyLog.v("Cookie " + cookie + " expired");
       expired = true;
     } else {
       if (cookieSpec.match(cookie, cookieOrigin)) {
         VolleyLog.v("Cookie " + cookie + " match " + cookieOrigin);
         matchedCookies.add(cookie);
       }
     }
   }
   // 有过期的Cookie存在,则去掉所有过期的Cookie
   if (expired) {
     cookieStore.clearExpired(now);
   }
   // -----------------------------------------------------------------------------------------------------
   // 4、将符合条件的Cookie字符串,设置成请求头headers Map中
   // -----------------------------------------------------------------------------------------------------
   String cookie = cookieSpec.formatCookies(matchedCookies);
   if (null != cookie && cookie.length() > 0) {
     cookieHeaders.put("Cookie", cookie);
   }
 }