mercredi 28 juillet 2021

OkHttp not sending tokens

I have OkHttp & Cookiejar handling all cookies. The instance of my client loads a login page, then responds with credentials and I get a 200 response with 4 cookies:

CONTOSO_USER
CONTOSO_INSTANCE
CONTOSO_ANALYTICS
JSESSIONID

The issue is that when I make subsequent requests to the server it appears its only sending JSESSIONID and CONTOSO_USER.

I am my client setup is:

   // COOKIE STORE
    CookieStore cookieStore = cookieManager.getCookieStore();

    // COOKIE JAR
    CookieJar cookieJar = new CookieJar() {
        private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
            cookieStore.put(url.host(), cookies);
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            List<Cookie> cookies = cookieStore.get(url.host());
            return cookies != null ? cookies : new ArrayList<Cookie>();
        }
    };

    // NEW CLIENT
    OkHttpClient client = new OkHttpClient.Builder()
        .addNetworkInterceptor(new LoggingInterceptor())
        .cookieJar(cookieJar)
        .build();
        
        // REQUEST TO LOGIN TO WEBSITE AND RECEIVE COOKIES

And then I can print the 4 cookies describe above with:

    for (Cookie cookie : cookieJar.loadForRequest(loginUrl)) {
        System.out.println("\n Cookie:" + cookie.name());
        System.out.println("\t Domain:" + cookie.domain());
        System.out.println("\t Value:" + cookie.value());
    }

Yet when I create a new request with:

    Request quickFind = new Request.Builder()
            .url(quickFIndUrl)
            .post(RequestBody.create(MediaType.parse("text/x-markdown"), postBody))
            .build();
     System.out.println(" Headers : " + quickFind.headers() +"\n") ;

It only sends JSESSIONID and CONTOSO_USER.

Why?




Aucun commentaire:

Enregistrer un commentaire