美文网首页SwiftUI学习笔记
安卓 Authorization 认证

安卓 Authorization 认证

作者: Attempt_30b7 | 来源:发表于2020-07-19 15:35 被阅读0次
    OkHttpClient okHttpClient = new OkHttpClient
            .Builder()
            .authenticator(new Authenticator() {
                @Nullable
                @Override
                public Request authenticate(@Nullable Route route, @NotNull Response response) {
    
                    return response.request().newBuilder()
                            .header("Authorization", credential)
                            .build();
                }
            })
            .build();
    
    MediaType JSON = MediaType.get("application/json; charset=utf-8");
    RequestBody body = RequestBody.create("", JSON);
    final Request request = new Request.Builder()
            .url(loginPostUrL)
            .addHeader("Charset", "UTF-8")
            .addHeader("Content-Type", "application/json")
            .addHeader("Authorization","Basic d2ViYXBwOndlYmFwcA==")
            .post(body)
            .build();
    
    
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
    
        }
    
        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
    
            Log.d("数据", response.body().string());
        }
    });
    

    必须输入账号和密码测试

    相关文章

      网友评论

        本文标题:安卓 Authorization 认证

        本文链接:https://www.haomeiwen.com/subject/umqmhktx.html