AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder()
.addResponseFilter(new ResponseFilter() {
@Override
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
String c = ctx.getResponseHeaders().getHeaders().get("Set-Cookie");
if (!TextUtils.isEmpty(c)) {
cookie = c;
System.out.println(cookie);
}
return ctx;
}
})
.build();
AsyncHttpClient client = new DefaultAsyncHttpClient(config);
配置的时候,拿到Set-Cookie请求头。只有登录的时候会有这个请求头。然后其他请求时,加上Cookie请求头
Response response = client
.preparePost("")
.addHeader("Cookie", cookie)
.execute().get();
本文介绍如何使用AsyncHttpClient进行配置以处理Set-Cookie响应头,并在后续请求中自动带上Cookie信息。通过实现自定义响应过滤器,可以在特定场景如登录验证后捕获并保存Cookie。
1262

被折叠的 条评论
为什么被折叠?



