Java CookieManager Cookie domain

The Domain Attribute

The Domain attribute specifies those hosts to which the cookie will
   be sent.  For example, if the value of the Domain attribute is
   "example.com", the user agent will include the cookie in the Cookie
   header when making HTTP requests to example.com, www.example.com, and
   www.corp.example.com.  (Note that a leading %x2E ("."), if present,
   is ignored even though that character is not permitted, but a
   trailing %x2E ("."), if present, will cause the user agent to ignore
   the attribute.)  If the server omits the Domain attribute, the user
   agent will return the cookie only to the origin server.

但是在Android开发过程中并非完全都准照上面协议,例如在Android上开发默认子域获取不到父域域名下的cookie值

假设定义:根域(example.com), 父域(id.example.com),子域(www.id.example.com)

不设置自定义的cookiePolicy,默认输出结论:

  1. 子域肯定能取到子域下的cookie;
  2. 子域不能取到父域下的cookie,但是在父域前面加·则可以取到
  3. 不管根域前加不加·,子域都不能根域下的cookie
package com.xiaomi.httptest;

import org.junit.Test;

import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CookieDomainTest {
    private static final String SUB_URL = "https://www.id.example.com";

    @Test
    public void parseDomain() throws Exception {

        List<String> cookies = new ArrayList<>();
        cookies.add("value1=uU1UCybATtSJurEl0; domain=example.com; path=/");
        cookies.add("value2=LneuU1UCybA; domain=.example.com; path=/; expires=Thu, 01-Dec-1994 16:00:00 GMT");
        cookies.add("value3=hoicVTZB1m7q4y; domain=id.example.com; path=/");
        cookies.add("value4=ZB1m7q4y4; domain=.id.example.com; path=/");
        cookies.add("value5=rykOwt9wL7O+FA2g4Fu; path=/");
        cookies.add("value6=hm7q4y4R/GXQk;domain=www.id.example.com;  path=/");

        Map<String, List<String>> headers = new HashMap<>();
        headers.put("Set-Cookie", cookies);
        URI subUri = URI.create(SUB_URL);

        CookieManager cm = new CookieManager();
        cm.put(subUri, headers);

        CookieStore cookieStore = cm.getCookieStore();

        List<HttpCookie> httpCookies = cookieStore.get(subUri);
        for (HttpCookie httpCookie : httpCookies) {
            System.out.println(SUB_URL + " cookie: " + httpCookie);
        }
    }
}

输出结果

https://www.id.example.com cookie: value5=rykOwt9wL7O+FA2g4Fu
https://www.id.example.com cookie: value6=hm7q4y4R/GXQk
https://www.id.example.com cookie: value4=ZB1m7q4y4

当然如果设置CookieManager的cookiePolicy定制如下,则会返回任何域有效的cookie值(expires过期的会自动过滤掉)

cm.setCookiePolicy(new CookiePolicy() {
            @Override
            public boolean shouldAccept(URI uri, HttpCookie cookie) {
                return true;
            }
        });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值