如何在Retrofit请求里添加Cookie
1个回答
2016-09-07
展开全部
你可以自定义一个RequestIntercaptor:
String cookieKey = ...
String cookieValue = ...
RestAdapter adapter = new RestAdapter.Builder()
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
// assuming `cookieKey` and `cookieValue` are not null
request.addHeader("Cookie", cookieKey + "=" + cookieValue);
}
})
.setServer("http://...")
.build();
YourService service = adapter.create(YourService.class);123456789101112131415
从服务器读取cookies再交给cookie manager管理:
OkHttpClient client = new OkHttpClient();
CustomCookieManager manager = new CustomCookieManager();
client.setCookieHandler(manager);
RestAdapter adapter = new RestAdapter.Builder()
.setClient(new OkClient(client))
...
.build();
String cookieKey = ...
String cookieValue = ...
RestAdapter adapter = new RestAdapter.Builder()
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
// assuming `cookieKey` and `cookieValue` are not null
request.addHeader("Cookie", cookieKey + "=" + cookieValue);
}
})
.setServer("http://...")
.build();
YourService service = adapter.create(YourService.class);123456789101112131415
从服务器读取cookies再交给cookie manager管理:
OkHttpClient client = new OkHttpClient();
CustomCookieManager manager = new CustomCookieManager();
client.setCookieHandler(manager);
RestAdapter adapter = new RestAdapter.Builder()
.setClient(new OkClient(client))
...
.build();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询