okhttp要求401 Unauthorized検証問題
1005 ワード
一、okhttpを使用する場合:
okhttp auth検証の追加
usernameとpasswordに対応するauthに必要なユーザー名パスワード:次の方法でユーザー名パスワードを取得できます.
二、社員をhttpclientさせる場合:
記事を参考にしてください.
https://blog.csdn.net/qq_27605885/article/details/79131483
okhttp auth検証の追加
public String post(String url, String json,String username,String password) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
String credential = Credentials.basic(username,password);//
Request request = new Request.Builder()
.header("Authorization", credential)// auth
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
usernameとpasswordに対応するauthに必要なユーザー名パスワード:次の方法でユーザー名パスワードを取得できます.
URL _url = new URL(url);
String[] userInfoArray =_url.getUserInfo().split(":");
String userName = userInfoArray[0];
String password = userInfoArray[1];
二、社員をhttpclientさせる場合:
記事を参考にしてください.
https://blog.csdn.net/qq_27605885/article/details/79131483