Accept All Cookies via HttpClient

后端 未结 2 688
陌清茗
陌清茗 2020-12-10 10:03

So this is currently how my app is set up:

1.) Login Activity. 2.) Once logged in, other activities may be fired up that use PHP scripts that require the cookies sen

相关标签:
2条回答
  • 2020-12-10 10:10

    The issue that you are facing seems to be by design for privacy/security purpose. In general any resource is not allowed to set a cookie it will not be able to receive. Here you are trying to set the cookie with the path trackallthethings from the resource /mobile-api/login.php which obviously is not working.

    Here you have following two options

    1. Set the cookie with the path which is accessible to both the resources (this may be root '/') OR
    2. Define a custom cookie policy and Registering your own cookie support. Here is related documentation and example.

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 10:29

    Since the API of HttpClient seems to change very fast, here is some working example code for HttpClient 4.5.1 to allow all (malformed) cookies:

    class EasyCookieSpec extends DefaultCookieSpec {
        @Override
        public void validate(Cookie arg0, CookieOrigin arg1) throws MalformedCookieException {
            //allow all cookies 
        }
    }
    
    class EasySpecProvider implements CookieSpecProvider {
        @Override
        public CookieSpec create(HttpContext context) {
            return new EasyCookieSpec();
        }
    }
    
    Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider>create()
                .register("easy", new EasySpecProvider())
                .build();
    
    CookieStore cookieStore = new BasicCookieStore();
    
    RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec("easy")
                .build();
    
    CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .setDefaultCookieSpecRegistry(r)
                .setDefaultRequestConfig(requestConfig)
                .build();
    
    0 讨论(0)
提交回复
热议问题