private static OkHttpClientImpl _getClient(CookieHandler cookieHandler) { OkHttpClientImpl okHttpClient = new OkHttpClientImpl(); //okHttpClient.setConnectTimeout(SET_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); //okHttpClient.setReadTimeout(SET_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS); //okHttpClient.setWriteTimeout(SET_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS); if (cookieHandler == null) { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE); okHttpClient._setCookieHandler(cookieManager); } else { okHttpClient._setCookieHandler(cookieHandler); } return okHttpClient; }
/** * Constructs a new HTTPInputGenerator using a Lua generation script. * The Lua script must contain the onInit() and onCall(callnum) functions. * onCall(callnum) must return the HTTP request for a specific call with number callnum. * callnum begins at 1 (Lua convention) and increments for each call. It resets back to 1 * if onCall returns nil. * @param scriptFile The url generator script. * @param randomSeed Seed for Lua random function. * @param timeout The http read timeout. */ public HTTPInputGenerator(File scriptFile, int randomSeed, int timeout) { OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); httpClientBuilder = httpClientBuilder.cookieJar(new JavaNetCookieJar(cookieManager)); if (timeout > 0) { httpClientBuilder = httpClientBuilder.readTimeout(timeout, TimeUnit.MILLISECONDS) .connectTimeout(timeout, TimeUnit.MILLISECONDS); } httpClient = httpClientBuilder.build(); if (scriptFile != null) { luaGlobals = JsePlatform.standardGlobals(); //luaGlobals.get("require").call(LuaValue.valueOf("tools.descartes.httploadgenerator.http.lua.HTML")); LuaValue library = new LuaTable(); library.set("getMatches", new GetMatches(htmlFunctions)); library.set("extractMatches", new ExtractAllMatches(htmlFunctions)); luaGlobals.set("html", library); luaGlobals.get("math").get("randomseed").call(LuaValue.valueOf(5)); luaGlobals.get("dofile").call(LuaValue.valueOf(scriptFile.getAbsolutePath())); } }
void doClientSide() throws Exception { // Wait for server to get started. while (!serverReady) { Thread.sleep(50); } HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; }}); URL url = new URL("https://localhost:" + serverPort +"/"); // Run without a CookieHandler first InputStream in = url.openConnection().getInputStream(); while (in.read() != -1); // read response body so connection can be reused // Set a CookeHandler and retest using the HttpClient from the KAC CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); in = url.openConnection().getInputStream(); while (in.read() != -1); if (manager.getCookieStore().getCookies().isEmpty()) { throw new RuntimeException("Failed: No cookies in the cookie Handler."); } }
CookieHttpClientTest() throws Exception { /* start the server */ ss = new ServerSocket(0); (new Thread(this)).start(); URL url = new URL("http://localhost:" + ss.getLocalPort() +"/"); // Run without a CookieHandler first InputStream in = url.openConnection().getInputStream(); while (in.read() != -1); // read response body so connection can be reused // Set a CookeHandler and retest using the HttpClient from the KAC CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); in = url.openConnection().getInputStream(); while (in.read() != -1); if (manager.getCookieStore().getCookies().isEmpty()) { throw new RuntimeException("Failed: No cookies in the cookie Handler."); } }
Call post(Callback callback) throws IOException { OkHttpClient client = getUnsafeOkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); RequestBody requestBody = new FormEncodingBuilder() .add("user_id", NetId) .add("user_password", password) .build(); Request request = new Request.Builder() .url("https://studentmaintenance.webapps.snu.edu.in/students/public/studentslist/studentslist/loginauth") .post(requestBody) .build(); Call call = client.newCall(request); call.enqueue(callback); return call; }
public void tstGetPost() throws Exception { String urlSource = "http://localhost:8080/beige-accounting-web/secure/main.jsp"; String urlBase = urlSource.substring(0, urlSource.indexOf("secure") - 1); assertEquals("http://localhost:8080/beige-accounting-web", urlBase); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); URL url = new URL(urlSource); Map<String, Object> params = new HashMap<String, Object>(); params.put("userName", "admin"); params.put("userPass", "admin"); params.put("authMethod", "form"); params.put("urlBase", urlBase); params.put("authUrl", urlBase + "/secure/j_security_check"); params.put("authUserName", "j_username"); params.put("authUserPass", "j_password"); params.put("urlSource", urlSource); tstAuthGet(url, params, cookieManager); //either Get or Post for test allowed urlSource = "http://localhost:8080/beige-accounting-web/secure/sendEntities"; params.put("urlSource", urlSource); //url = new URL(urlSource); //tstAuthPost(url, params, cookieManager); }
private OkHttpClientManager() { mOkHttpClient = new OkHttpClient(); //cookie enabled mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); mDelivery = new Handler(Looper.getMainLooper()); mGson = new Gson(); //just for test if (false) { mOkHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); } }
private OkHttpUtils() { mOkHttpClient = new OkHttpClient(); //cookie enabled mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); mDelivery = new Handler(Looper.getMainLooper()); if (true) { mOkHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); } }
private static void setup() { baseUrlManager = new BaseUrlManager(UrlConstants.SKS1_BASE); okHttpClient = new OkHttpClient(); cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); okHttpClient.setCookieHandler(cookieManager); okHttpClient.setConnectTimeout(5, TimeUnit.SECONDS); okHttpClient.setReadTimeout(5, TimeUnit.SECONDS); okHttpClient.setWriteTimeout(5, TimeUnit.SECONDS); okHttpClient.interceptors().add(new HandlerResponseInterceptor()); retrofit = new Retrofit.Builder() .baseUrl(baseUrlManager) .client(okHttpClient) .addConverterFactory(new DocumentConverterFactory()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); service = retrofit.create(ConnectionService.class); }
public static void apiInit() { try { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cookieJar(new JavaNetCookieJar(cookieManager)) .build(); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .build(); // Service setup apiService = retrofit.create(CodenvyApiService.class); } catch (Exception e) { e.printStackTrace(); } }
public CoreServices(Context context) { this.context = context; // Storage sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); cookieStore = new PersistentCookieStore(context); // General userManager = new UserManager(sharedPrefs); errorHandler = new ErrorHandler(context); localBroadcastManager = LocalBroadcastManager.getInstance(context); // Network firebaseRef = new Firebase(Constants.FIREBASE_URL); okHttpClient = new OkHttpClient(); okHttpClient.setCookieHandler(new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL)); // Tasks codementorTasks = new CodementorTasks(okHttpClient); firebaseTasks = new FirebaseTasks(firebaseRef, codementorTasks, userManager); }
OkHttpClient provideHttpClient() { OkHttpClient.Builder builder = new OkHttpClient().newBuilder() .connectTimeout(30, TimeUnit.SECONDS); builder.addInterceptor(new KinopoiskRequestInterceptor()); // Log http requests on debug if (BuildConfig.DEBUG) { //logging interceptor should be last interceptor in chain HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(loggingInterceptor); } CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); builder.cookieJar(new JavaNetCookieJar(cookieManager)); return builder.build(); }
private OkNet() { OkHttpClient.Builder builder = client.newBuilder(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE); builder.cookieJar(new JavaNetCookieJar(cookieManager)); builder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); builder.connectTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(20, TimeUnit.SECONDS); client = builder.build(); }
/** * Creates an instance of the builder with a base URL and 2 custom builders. * * @param httpClientBuilder the builder to build an {@link OkHttpClient}. * @param retrofitBuilder the builder to build a {@link Retrofit}. */ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { if (httpClientBuilder == null) { throw new IllegalArgumentException("httpClientBuilder == null"); } if (retrofitBuilder == null) { throw new IllegalArgumentException("retrofitBuilder == null"); } CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); customHeadersInterceptor = new CustomHeadersInterceptor(); // Set up OkHttp client this.httpClientBuilder = httpClientBuilder .cookieJar(new JavaNetCookieJar(cookieManager)) .readTimeout(60, TimeUnit.SECONDS) .addInterceptor(new RequestIdHeaderInterceptor()) .addInterceptor(new BaseUrlHandler()); this.retrofitBuilder = retrofitBuilder; this.loggingInterceptor = new LoggingInterceptor(LogLevel.NONE); }
private HttpManager() { this.mOkHttpClient = new OkHttpClient(); //cookie enabled mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); mDelivery = new Handler(Looper.getMainLooper()); final int sdk = Build.VERSION.SDK_INT; if (sdk >= 23) { GsonBuilder gsonBuilder = new GsonBuilder() .excludeFieldsWithModifiers( Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC); mGson = gsonBuilder.create(); } else { mGson = new Gson(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); android.webkit.CookieManager.getInstance().setAcceptCookie(true); CookieManager cookieManager = new CookieManager(new PersistentCookieStore(getApplicationContext()), CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); new APITask(new APICallback() { @Override public void r(String result) { if (result.length() != 0) { Intent queueList = new Intent(MainActivity.this, QueueListActivity.class); startActivity(queueList); } else { setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } } }).execute("method", "userData"); }
public static void main(String[] args) { AppVars.setAppVars(); Preferences.load(); if (Preferences.useSystemLookAndFeel()) { useSystemLookAndFeel(true); } CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); if (Preferences.useUserProxy()) { ComicUtil.setProxy(Preferences.getProxyAddress(), Preferences.getProxyPort(), Preferences.getProxyUsername(), Preferences.getProxyEncryptedPassword()); } ComicUtil.getNewestComic(); EventQueue.invokeLater(() -> { MainFrame frame = new MainFrame("XKCDownload"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setLocationRelativeTo(null); }); }
public static void initCookieManager() { CookieHandler cookieHandler = CookieHandler.getDefault(); if (cookieHandler == null) { // CookieStore cookieStore = CookieStoreHolder.initInMemoryCookieStore(); // CookieManager cm = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL); CookieManager cm = new CookieManager(); cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cm); } // else { // CookieStoreHolder.initInMemoryCookieStore(); // } }
public LoginService() { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); mPersistentCookieStore = new PersistentCookieStore(MyApplication.getContextObject()); mClient = new OkHttpClient.Builder() .cookieJar(new CustomCookieJar(new CookieManager(mPersistentCookieStore, CookiePolicy.ACCEPT_ALL))) .readTimeout(12, TimeUnit.SECONDS) .addInterceptor(htmlInterceptor) .addInterceptor(loggingInterceptor) .build(); mGson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") .serializeNulls() .create(); }
@Test public void controllerKioskMode() throws Exception { // setup Messages messages = new Messages(Locale.getDefault()); Runtime runtime = new TestRuntime(); View view = new TestView(); Controller controller = new Controller(view, runtime, messages); // make sure that the session cookies are passed during conversations CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); this.servletHolder.setInitParameter("Kiosk", "true"); // operate controller.run(); // verify LOG.debug("verify..."); }
@Provides @Singleton OkHttpClient provideOkHttpClient(Context context) { OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); try { Cache cache = new Cache(context.getCacheDir(), 10 * 1024 * 1024); // 10 MiB client.setCache(cache); } catch (IOException e) { Toast.makeText(context, "Cannot create cache for http responses", Toast.LENGTH_SHORT).show(); } return client; }
private OkHttpClientManager() { mOkHttpClient = new OkHttpClient(); // cookie enabled mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); mDelivery = new Handler(Looper.getMainLooper()); mGson = new Gson(); /*just for test !!!*/ mOkHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); }
@Override public void onCreate() { super.onCreate(); RealmConfiguration configuration = new RealmConfiguration.Builder(this).build(); Realm.setDefaultConfiguration(configuration); sInstance = this; context = getApplicationContext(); cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); client = new OkHttpClient.Builder() .cookieJar(new JavaNetCookieJar(cookieManager)) .connectTimeout(5, TimeUnit.SECONDS) .build(); Stetho.initialize( Stetho.newInitializerBuilder(this) .enableDumpapp(Stetho.defaultDumperPluginsProvider(this)) .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build()) .build()); }
public static OkHttpClient getsInstance() { if (sInstance == null) { synchronized (OkHttpClientManager.class) { if (sInstance == null) { sInstance = new com.squareup.okhttp.OkHttpClient(); // cookie enabled sInstance.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); // timeout reading data from the host sInstance.setReadTimeout(15, TimeUnit.SECONDS); // timeout connection host sInstance.setConnectTimeout(20, TimeUnit.SECONDS); } } } return sInstance; }
@Before public void init() { trash = new ArrayList<T>(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder// .setDateFormat("yyyy-MM-dd HH:mm:ss");// Gson gson = gsonBuilder.create(); RestAdapter restAdapter = new RestAdapter.Builder() // تعیین مبدل داده .setConverter(new GsonConverter(gson)) // تعیین کنترل کننده خطا .setErrorHandler(new PErrorHandler()) // تعیین آدرس سایت مورد نظر .setEndpoint(TestSettings.getApiURL()) // ایجاد یک نمونه .build(); // ایجاد سرویسها this.userService = restAdapter.create(IRUserService.class); createTService(restAdapter); }
@Before public void createService() { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder// .setDateFormat("yyyy-MM-dd HH:mm:ss");// Gson gson = gsonBuilder.create(); RestAdapter restAdapter = new RestAdapter.Builder()// .setConverter(new GsonConverter(gson))// // تعیین کنترل کننده خطا .setErrorHandler(new PErrorHandler()) // تعیین آدرس سایت مورد نظر .setEndpoint(API_URL) // ایجاد یک نمونه .build(); this.profileAdmin = restAdapter.create(IRProfileAdministrator.class); this.usr = restAdapter.create(IRUserService.class); }
public PUserDaoRetrofitTest() { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder// .setDateFormat("yyyy-MM-dd HH:mm:ss");// Gson gson = gsonBuilder.create(); RestAdapter restAdapter = new RestAdapter.Builder()// .setConverter(new GsonConverter(gson))// .setErrorHandler(new PErrorHandler())// .setEndpoint(API_URL)// .build(); this.userSerivece = restAdapter.create(IRUserService.class); userDao = new PUserDaoRetrofit(); userDao.setUserService(userSerivece); }
@Before public void init() { trash = new ArrayList<PContent>(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder// .setDateFormat("yyyy-MM-dd HH:mm:ss");// Gson gson = gsonBuilder.create(); RestAdapter restAdapter = new RestAdapter.Builder() // تعیین مبدل داده .setConverter(new GsonConverter(gson)) // تعیین کنترل کننده خطا .setErrorHandler(new PErrorHandler()) // تعیین آدرس سایت مورد نظر .setEndpoint(TestSettings.getApiURL()) // ایجاد یک نمونه .build(); // ایجاد سرویسها this.userService = restAdapter.create(IRUserService.class); this.contentService = restAdapter.create(IContentService.class); }
private static RequestQueue newRequestQueue() { VolleyLog.DEBUG = true; RequestQueue requestQueue = new RequestQueue(openCache(), new BasicNetwork(new HurlStack())); requestQueue.start(); CookieManager manager = new CookieManager(new SimpleCookieStore(), CookiePolicy.ACCEPT_ORIGINAL_SERVER); CookieHandler.setDefault(manager); // String userAgent = "volley/0"; // try { // String packageName = BaseApplication.getBaseApplication().getPackageName(); // PackageInfo info = BaseApplication.getBaseApplication().getPackageManager().getPackageInfo(packageName, 0); // userAgent = packageName + "/" + info.versionCode; // } catch (PackageManager.NameNotFoundException e) {} // android.net.http.AndroidHttpClient httpClient = AndroidHttpClient.newInstance(userAgent); // HttpStack httpStack = new OwnHttpClientStack(httpClient); // RequestQueue requestQueue = Volley.newRequestQueue(BaseApplication.getBaseApplication(), // httpStack); return requestQueue; }
public SensableUser(SharedPreferences sharedPreferences, Context context) { this.sharedPreferences = sharedPreferences; this.context = context; mUser = new User(); loggedIn = readUserFromPreferences(); //TODO: Remove this once API doesn't use cookies CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); RestAdapter restAdapter = new RestAdapter.Builder() .setLogLevel(RestAdapter.LogLevel.FULL) .setEndpoint("http://sensable.io") .build(); service = restAdapter.create(SensableService.class); }
@Override public void onCreate() { super.onCreate(); // cookie store CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); // init cache for http responses try { File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch(IOException e){ Log.i(TAG, "HTTP response cache installation failed:" + e); } }