A
A
artshelom2017-08-24 20:04:46
Java
artshelom, 2017-08-24 20:04:46

How to view cookies??

How to view cookies when replying? and does it return cookies at all?
I use OkHttp, because I'm doing an android application and there should be a library without problems

OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();

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

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

        Request request = new Request.Builder()
                .url("https://toster.ru")
                .build();
        String html = null;
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            html = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Cookie> list = client.cookieJar().loadForRequest(HttpUrl.parse("toster.ru"));
        System.out.println(list.size() + " Cookie");//Показывает 0

It feels like there are either no cookies in the response or they are not recorded)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Myvrenik, 2017-08-24
@artshelom

If we look at the headers that https://toster.ru/ returns, we will see the following:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html;charset=utf-8
Date: Thu, 24 Aug 2017 17:14:51 GMT
Keep-Alive: timeout=15
Public-Key-Pins: pin-sha256="klO23nT2ehFDXCfx3eHTDRESMz3asj1muO+4aIdjiuY=";pin-sha256="ATPF8U6AdEXM7aD9/PTAJldZj9jI6NWEvRGMbDJiN3g="; max-age=15552000
Server: QRATOR
Strict-Transport-Security: max-age=31536000
Transfer-Encoding: chunked
X-Powered-By: PHP/5.6.20-1+deb.sury.org~trusty+1

As you can see, Toaster doesn't set cookies right away. This can be seen from the absence of the Set-Cookie header . It would be strange if your code found some cookies.
Replace https://toster.ru and toster.ru with https://github.com , for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question