Authenticator authenticator = (Route route, Response response) -> {
String credential = Credentials.basic("name", "password");
return response.request().newBuilder().header("Authorization", credential).build();
};
OkHttpClient okHttpClient = new OkHttpClient.Builder().authenticator(authenticator).build();
RequestBody body = RequestBody.create(MEDIA_JSON, request.toJSONString());
Request okRequest = new Request.Builder()
.url("url")
.post(body)
.build();
try {
Response response = okHttpClient.newCall(okRequest).execute();
} catch (IOException e) {
logger.error("请求失败", e);
}
OkHttp Basic Authentication
最新推荐文章于 2024-09-05 15:52:51 发布
本文介绍如何使用OkHttp库进行HTTP请求的身份验证,包括设置认证信息、构建OkHttpClient实例、创建请求体及发送POST请求的过程。
3453

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



