okhttp3带cookie请求

服务器使用shiro做登录校验及权限验证的时候

app端访问时就要带上cookie,不然请求会提示未登录

private void postAsynHttp() {
        mOkHttpClient=new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    private final HashMap> cookieStore = new HashMap<>();

                    @Override
                    public void saveFromResponse(HttpUrl url, List cookies) {
                        cookieStore.put(url, cookies);
                        cookieStore.put(HttpUrl.parse("http://192.168.31.231:8080/shiro-2"), cookies);
                        for(Cookie cookie:cookies){
                            System.out.println("cookie Name:"+cookie.name());
                            System.out.println("cookie Path:"+cookie.path());
                        }
                    }

                    @Override
                    public List loadForRequest(HttpUrl url) {
                        List cookies = cookieStore.get(HttpUrl.parse("http://192.168.31.231:8080/shiro-2"));
                        if(cookies==null){
                            System.out.println("没加载到cookie");
                        }
                        return cookies != null ? cookies : new ArrayList();
                    }
                })
                .build();
        RequestBody formBody = new FormBody.Builder()
                .add("username", "admin")
                .add("password", "admin")
                .build();
        final Request request = new Request.Builder()
                .url("http://192.168.31.231:8080/shiro-2/shiro-login")
                .post(formBody)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.i("wangshu", str);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showProgress(false);
                        Toast.makeText(getApplicationContext(), "请求成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }

        });
    }

使用上面逻辑登录后会缓存cookie;

之后使用同一mOkHttpClient做请求访问

 public void test(View v){
        final Request request = new Request.Builder()
                .url("http://192.168.31.231:8080/shiro-2/user.jsp")
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.i("wangshu", str);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showProgress(false);
                        Toast.makeText(getApplicationContext(), "请求成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }

        });
    }


你可能感兴趣的:(android)