简体   繁体   中英

Okhttp3 get cookie

I am doing this request :

    OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
    RequestBody body = RequestBody.create(mediaType, "username=George&password=toto");
    Request request = new Request.Builder()
            .url("http://localhost:8080/login")
            .post(body)
            .addHeader("content-type", "application/x-www-form-urlencoded")
            .build();
    try {
        Response response = client.newCall(request).execute();

    } catch (IOException e) {
        e.printStackTrace();
    }

But how to get the cookies that the server returned me ? I just want to store it on a String variable (that's all)

Thank you in advance !

The idea here is to create or use an existing CookieJar implementation and then read the cookies from that. See this answer for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM