Add cookie to OkHttp client request

So, I started using Okhttp 3 and most of the examples on the Internet about older versions

I need to add a cookie to OkHttp client requests, how is this done with OkHttp 3?

In my case, I just want to statically add it to client calls without getting them from the server

+6
source share
5 answers

There are two ways to do this:

OkHttpClient client = new OkHttpClient().newBuilder() .cookieJar(new CookieJar() { @Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { } @Override public List<Cookie> loadForRequest(HttpUrl url) { final ArrayList<Cookie> oneCookie = new ArrayList<>(1); oneCookie.add(createNonPersistentCookie()); return oneCookie; } }) .build(); ... public static Cookie createNonPersistentCookie() { return new Cookie.Builder() .domain("publicobject.com") .path("/") .name("cookie-name") .value("cookie-value") .httpOnly() .secure() .build(); } 

or simply

 OkHttpClient client = new OkHttpClient().newBuilder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { final Request original = chain.request(); final Request authorized = original.newBuilder() .addHeader("Cookie", "cookie-name=cookie-value") .build(); return chain.proceed(authorized); } }) .build(); 

I have a feeling that the second sentence is what you need.

You can find a working example here .

+15
source

If you need to set a cookie for a single request, you can simply add the header:

 Request request = new Request.Builder() .addHeader("Cookie", "yourcookie") .url("http://yoursite.com") .build(); 

Otherwise, if you want to read the cookies returned by the server and attach them to other requests, you will need a CookieJar . For Android, you can use the PersistentCookieJar library , which processes cookies correctly and also saves them in the general settings:

 ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cookieJar(cookieJar) .build(); 
+3
source

I had the same needs, I created my own library. You can force cookies such as OkHttp3CookieHelper to https://github.com/riversun/okhttp3-cookie-helper .

  String url = "https://example.com/webapi"; OkHttp3CookieHelper cookieHelper = new OkHttp3CookieHelper(); cookieHelper.setCookie(url, "cookie_name", "cookie_value"); OkHttpClient client = new OkHttpClient.Builder() .cookieJar(cookieHelper.cookieJar()) .build(); Request request = new Request.Builder() .url(url) .build(); 

Gradle

 compile 'org.riversun:okhttp3-cookie-helper:1.0.0' 

Maven

 <dependency> <groupId>org.riversun</groupId> <artifactId>okhttp3-cookie-helper</artifactId> <version>1.0.0</version> </dependency> 
+1
source

I think the best way to do this is to add a cookie to the cookieJar. OkHttp will automatically add cookies to the request using an interceptor: https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/http/BridgeInterceptor.java

 cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager); // add cookies to cookiejar cookieJar.saveFromResponse("Cookie", listOfCookiesToAdd); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.cookieJar(cookieJar) 

I really have not tried this code, but it should work.

0
source

You must set the headers in the interface declaration, for example: Call login (@Header ("Cookie") String sessionId ...)

0
source

All Articles