cookie

来源:互联网 发布:知乎话题广场 编辑:程序博客网 时间:2024/06/06 16:36
public static void sendPost(final Context context, RequestParams params, final boolean isSaveCookie, final UtilHttpListenerInterface listener) {    /*取cookie*/    String value = context.getSharedPreferences("SP_NAME",Context.MODE_PRIVATE).getString("value","");    String domain = context.getSharedPreferences("SP_NAME",Context.MODE_PRIVATE).getString("domain","");    String path = context.getSharedPreferences("SP_NAME",Context.MODE_PRIVATE).getString("path","");    if (!value.equals("")){        strCookie.append(String.format("SHAREJSESSIONID=%s", value));        strCookie.append(String.format(";domain=%s", domain));        strCookie.append(String.format(";path=%s",path));        /*传cookie*/        params.addHeader("Cookie", strCookie.toString());    }    x.http().post(params, new Callback.CommonCallback<String>() {        @Override        public void onCancelled(CancelledException arg0) {        }        @Override        public void onError(Throwable ex, boolean isOnCallback) {            if (ex instanceof HttpException) { // 网络错误                HttpException httpException = (HttpException) ex;                listener.onFailed(httpException.getCode(), httpException.getMessage(), httpException.getResult());            } else { // 其他错误            }        }        // 不管成功或者失败最后都会回调该接口        @Override        public void onFinished() {        }        @Override        public void onSuccess(String result) {            if (isSaveCookie) {                /*取cookie*/                DbCookieStore instance = DbCookieStore.INSTANCE;                List<HttpCookie> cookies = instance.getCookies();                for (int i = 0; i < cookies.size(); i++) {                    HttpCookie cookie = cookies.get(i);                    String name = cookie.getName();                    String value = cookie.getValue();                    String domain = cookie.getDomain();                    String path = cookie.getPath();                    Log.d("aaa",name+value);                    if (name.equals("JSESSIONID")){                        /*存cookie*/                        SharedPreferences.Editor editor = context.getSharedPreferences("SP_NAME", context.MODE_PRIVATE).edit();                        editor.putString("value", value);                        editor.putString("domain", domain);                        editor.putString("path", path);                        editor.commit();                    }                  ;                    sbCookie.append(String.format("SHAREJSESSIONID=%s", cookie.getValue()));                    sbCookie.append(String.format(";domain=%s", cookie.getDomain()));                    sbCookie.append(String.format(";path=%s", cookie.getPath()));                    String cookieValue = sbCookie.toString();                }            }            listener.onSucceed(result);        }    });}
0 0
原创粉丝点击