android客户端cookies,android – 将cookie添加到客户端请求OkHttp
有兩種方法可以做到這一點(diǎn):
OkHttpClient client = new OkHttpClient().newBuilder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List cookies) {
}
@Override
public List loadForRequest(HttpUrl url) {
final ArrayList 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();
}
或簡(jiǎn)單地說(shuō)
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();
我覺(jué)得第二個(gè)建議是你需要的.
您可以找到here個(gè)工作示例.
總結(jié)
以上是生活随笔為你收集整理的android客户端cookies,android – 将cookie添加到客户端请求OkHttp的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux建立动态库链接,Linux动态
- 下一篇: mysql dns反向解析_Mysql