com.franmontiel.persistentcookiejar.PersistentCookieJar Java Examples
The following examples show how to use
com.franmontiel.persistentcookiejar.PersistentCookieJar.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpHelper.java From CrazyDaily with Apache License 2.0 | 6 votes |
@Inject public HttpHelper(Context context) { if (mOkHttpClient == null) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); // 设置缓存 20M Cache cache = new Cache(new File(context.getExternalCacheDir(), CacheConstant.CACHE_DIR_API), 20 * 1024 * 1024); builder.cache(cache); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLogger()); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(loggingInterceptor); builder.addInterceptor(new CrazyDailyCacheInterceptor()); builder.addNetworkInterceptor(new CrazyDailyCacheNetworkInterceptor()); // 设置Cookie builder.cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context))); // 设置超时 builder.connectTimeout(10, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); builder.writeTimeout(20, TimeUnit.SECONDS); // 错误重连 builder.retryOnConnectionFailure(true); mOkHttpClient = builder.build(); } }
Example #2
Source File: app.java From ClassSchedule with Apache License 2.0 | 6 votes |
private void initOkHttp() { ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext())); Cache.instance().setCookieJar(cookieJar); OkHttpClient okHttpClient = new OkHttpClient.Builder() //.followRedirects(false) //禁制OkHttp的重定向操作,我们自己处理重定向 .followSslRedirects(false) //.cookieJar(new LocalCookieJar()) //为OkHttp设置自动携带Cookie的功能 .addInterceptor(new LoggerInterceptor("TAG")) //.cookieJar(new CookieJarImpl(new PersistentCookieStore(getBaseContext()))) //要在内存Cookie前 //.cookieJar(new CookieJarImpl(new MemoryCookieStore()))//内存Cookie .cookieJar(cookieJar) .cache(null) .build(); OkHttpUtils.initClient(okHttpClient); }
Example #3
Source File: Global.java From NClientV2 with Apache License 2.0 | 5 votes |
public static void reloadHttpClient(@NonNull Context context){ OkHttpClient.Builder builder=new OkHttpClient.Builder() .cookieJar( new PersistentCookieJar( new SetCookieCache(), new SharedPrefsCookiePersistor(context.getSharedPreferences("Login",0)) ) ); CustomSSLSocketFactory.enableTls12OnPreLollipop(builder); builder.addInterceptor(new CustomInterceptor()); client=builder.build(); client.dispatcher().setMaxRequests(25); client.dispatcher().setMaxRequestsPerHost(25); for(Cookie cookie:client.cookieJar().loadForRequest(HttpUrl.get(Utility.getBaseUrl()))){ LogUtility.d("Cookie: "+cookie); } if(Login.isLogged()&&Login.getUser()==null){ User.createUser(user -> { if(user!=null){ new LoadTags(null).start(); if(context instanceof MainActivity){ ((MainActivity) context).runOnUiThread(() -> ((MainActivity)context).loginItem.setTitle(context.getString(R.string.login_formatted,user.getUsername()))); } } }); } }
Example #4
Source File: HttpClient.java From AndroidModulePattern with Apache License 2.0 | 5 votes |
private HttpClient() { ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(Utils.getContext())); //HttpsUtil.SSLParams sslParams = HttpsUtil.getSslSocketFactory(Utils.getContext(), R.raw.cer,STORE_PASS , STORE_ALIAS); okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) //.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) // .hostnameVerifier(HttpsUtil.getHostnameVerifier()) .addInterceptor(new LoggerInterceptor(null, true)) .cookieJar(cookieJar) .build(); }
Example #5
Source File: MyApplication.java From MyOkHttp with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); mInstance = this; //持久化存储cookie ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext())); //log拦截器 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); //自定义OkHttp OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .cookieJar(cookieJar) //设置开启cookie .addInterceptor(logging) //设置开启log .build(); mMyOkHttp = new MyOkHttp(okHttpClient); //默认 // mMyOkHttp = new MyOkHttp(); mDownloadMgr = (DownloadMgr) new DownloadMgr.Builder() .myOkHttp(mMyOkHttp) .maxDownloadIngNum(5) //设置最大同时下载数量(不设置默认5) .saveProgressBytes(50 * 1204) //设置每50kb触发一次saveProgress保存进度 (不能在onProgress每次都保存 过于频繁) 不设置默认50kb .build(); mDownloadMgr.resumeTasks(); //恢复本地所有未完成的任务 }
Example #6
Source File: RetrofitFactory.java From Toutiao with Apache License 2.0 | 5 votes |
@NonNull public static Retrofit getRetrofit() { if (retrofit == null) { synchronized (RetrofitFactory.class) { if (retrofit == null) { // 指定缓存路径,缓存大小 50Mb Cache cache = new Cache(new File(InitApp.AppContext.getCacheDir(), "HttpCache"), 1024 * 1024 * 50); // Cookie 持久化 ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(InitApp.AppContext)); OkHttpClient.Builder builder = new OkHttpClient.Builder() .cookieJar(cookieJar) .cache(cache) .addInterceptor(cacheControlInterceptor) .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) .retryOnConnectionFailure(true); // Log 拦截器 if (BuildConfig.DEBUG) { builder = SdkManager.initInterceptor(builder); } retrofit = new Retrofit.Builder() .baseUrl(INewsApi.HOST) .client(builder.build()) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } } } return retrofit; }
Example #7
Source File: MyApplication.java From okhttputils with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext())); HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null); // CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore()); OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .addInterceptor(new LoggerInterceptor("TAG")) .cookieJar(cookieJar1) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }) .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) .build(); OkHttpUtils.initClient(okHttpClient); }
Example #8
Source File: ShortyzApplication.java From shortyz with GNU General Public License v3.0 | 5 votes |
public CookieJar getCookieJar(){ if(this.cookieJar.get() == null){ this.cookieJar.compareAndSet(null, new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(this))); } return this.cookieJar.get(); }
Example #9
Source File: AppCookieManager.java From v9porn with MIT License | 4 votes |
@Inject public AppCookieManager(SharedPrefsCookiePersistor sharedPrefsCookiePersistor, SetCookieCache setCookieCache, PersistentCookieJar persistentCookieJar) { this.sharedPrefsCookiePersistor = sharedPrefsCookiePersistor; this.setCookieCache = setCookieCache; this.persistentCookieJar = persistentCookieJar; }
Example #10
Source File: ApiServiceModule.java From v9porn with MIT License | 4 votes |
@Singleton @Provides PersistentCookieJar providesPersistentCookieJar(SharedPrefsCookiePersistor sharedPrefsCookiePersistor, SetCookieCache setCookieCache) { return new PersistentCookieJar(setCookieCache, sharedPrefsCookiePersistor); }
Example #11
Source File: AppCookieManager.java From v9porn with MIT License | 4 votes |
@Inject public AppCookieManager(SharedPrefsCookiePersistor sharedPrefsCookiePersistor, SetCookieCache setCookieCache, PersistentCookieJar persistentCookieJar) { this.sharedPrefsCookiePersistor = sharedPrefsCookiePersistor; this.setCookieCache = setCookieCache; this.persistentCookieJar = persistentCookieJar; }
Example #12
Source File: ApiServiceModule.java From v9porn with MIT License | 4 votes |
@Singleton @Provides PersistentCookieJar providesPersistentCookieJar(SharedPrefsCookiePersistor sharedPrefsCookiePersistor, SetCookieCache setCookieCache) { return new PersistentCookieJar(setCookieCache, sharedPrefsCookiePersistor); }
Example #13
Source File: HttpModule.java From Awesome-WanAndroid with Apache License 2.0 | 4 votes |
@Singleton @Provides OkHttpClient provideClient(OkHttpClient.Builder builder) { // 网络请求质量监控 builder.eventListenerFactory(OkHttpEventListener.FACTORY); // httpDns 优化 builder.dns(OkHttpDns.getIns(WanAndroidApp.getAppComponent().getContext())); File cacheFile = new File(Constants.PATH_CACHE); Cache cache = new Cache(cacheFile, 1024 * 1024 * 50); Interceptor cacheInterceptor = chain -> { Request request = chain.request(); if (!CommonUtils.isNetworkConnected()) { // 无网时强制使用数据缓存,以提升用户体验。 request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } Response response = chain.proceed(request); if (CommonUtils.isNetworkConnected()) { int maxAge = 0; // 有网络时, 不缓存, 最大保存时长为0 response.newBuilder() .header("Cache-Control", "public, max-age=" + maxAge) .removeHeader("Pragma") .build(); } else { // 无网络时,设置超时为4周 int maxStale = 60 * 60 * 24 * 28; response.newBuilder() .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) .removeHeader("Pragma") .build(); } return response; }; // 缓存优化 builder.addNetworkInterceptor(cacheInterceptor); builder.addInterceptor(cacheInterceptor); builder.cache(cache); if (BuildConfig.DEBUG) { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); builder.addInterceptor(loggingInterceptor); builder.addNetworkInterceptor(new StethoInterceptor()); } //设置超时 builder.connectTimeout(10, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); builder.writeTimeout(20, TimeUnit.SECONDS); //错误重连 builder.retryOnConnectionFailure(true); //cookie认证 builder.cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(WanAndroidApp.getInstance()))); return RetrofitUrlManager.getInstance().with(builder).build(); }
Example #14
Source File: HttpClientFactory.java From mimi-reader with Apache License 2.0 | 4 votes |
private void init(ClientType type) { final Cache cache = new Cache(MimiUtil.getInstance().getCacheDir(), MAX_CACHE_SIZE); final OkHttpClient.Builder builder = new OkHttpClient.Builder(); cookiePersistor = new SharedPrefsCookiePersistor(MimiApplication.getInstance()); PersistentCookieJar jar = new PersistentCookieJar(new SetCookieCache(), cookiePersistor); builder.cache(cache) .cookieJar(jar) .connectTimeout(90, TimeUnit.SECONDS) .readTimeout(90, TimeUnit.SECONDS) .writeTimeout(90, TimeUnit.SECONDS) .retryOnConnectionFailure(false) .addNetworkInterceptor(headerInterceptor()); if (BuildConfig.DEBUG) { builder.addNetworkInterceptor(loggingInterceptor()); builder.addNetworkInterceptor(new StethoInterceptor()); } // try { // SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); // builder.sslSocketFactory(sslSocketFactory, new MyX509TrustManager()); // } catch (NoSuchAlgorithmException | KeyStoreException e) { // Log.e(LOG_TAG, "Could not enable SSL", e); // Crashlytics.logException(e); // } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MimiApplication.getInstance()); String prefsKey = MimiApplication.getInstance().getString(R.string.http_buffer_size_pref); String defaultValue = MimiApplication.getInstance().getString(R.string.http_buffer_size_default); int bufferSize = Integer.parseInt(prefs.getString(prefsKey, defaultValue)); if (bufferSize > 0) { builder.socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) { @Override protected Socket configureSocket(Socket socket) throws IOException { socket.setSendBufferSize(bufferSize); socket.setReceiveBufferSize(bufferSize); return socket; } }); } client = builder.build(); }
Example #15
Source File: NetManager.java From Aria with Apache License 2.0 | 4 votes |
private void init() { mCookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(BaseApp.context)); //OkHttpClient okHttpClient = provideOkHttpClient(); okHttpClient = provideOkHttpClient(); }