这应该很简单,但我必须遗漏一些东西。所以有一个授权网址,我需要将用户名和密码传递给:
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
HttpUrl.Builder urlBuilder = HttpUrl.parse(AUTHTOKEN_URL).newBuilder();
urlBuilder.addQueryParameter("username", AUTHTOKEN_USERNAME);
urlBuilder.addQueryParameter("password", AUTHTOKEN_PASSWORD);
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
当我调用 AUTHTOKEN_URL 时,结果为“OK”,但 saveFromResponse 似乎从未被调用,所以我认为有些事情是不对的,因为当我对另一个依赖于此身份验证令牌的服务器进行下一次 http 调用时,它不会返回任何数据。