Search in sources :

Example 11 with CookieJar

use of okhttp3.CookieJar in project instagram-java-scraper by postaddictme.

the class AnonymousInstaTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new UserAgentInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    client = new Instagram(httpClient);
    client.basePage();
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) UserAgentInterceptor(me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

Example 12 with CookieJar

use of okhttp3.CookieJar in project instagram-java-scraper by postaddictme.

the class AuthenticatedInstaTest method testLoginWithInvalidCredentials.

@Test(expected = InstagramAuthException.class)
public void testLoginWithInvalidCredentials() throws Exception {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new UserAgentInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    Instagram instagramClient = new Instagram(httpClient);
    instagramClient.basePage();
    instagramClient.login("1", "2");
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) UserAgentInterceptor(me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Test(org.junit.Test)

Example 13 with CookieJar

use of okhttp3.CookieJar in project instagram-java-scraper by postaddictme.

the class AuthenticatedInstaTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    Credentials credentials = new Credentials();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new UserAgentInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    client = new Instagram(httpClient);
    client.basePage();
    client.login(credentials.getLogin(), credentials.getPassword());
    client.basePage();
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) UserAgentInterceptor(me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

Example 14 with CookieJar

use of okhttp3.CookieJar in project instagram-java-scraper by postaddictme.

the class MultiThreadTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new UserAgentInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    client = new Instagram(httpClient);
    client.basePage();
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) UserAgentInterceptor(me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

Example 15 with CookieJar

use of okhttp3.CookieJar in project YhLibraryForAndroid by android-coco.

the class MyApplication method initHttp.

/**
 * 初始化OKHTTP
 */
public static void initHttp() {
    // 全局设置请求头  单独设置请求头覆盖全局设置
    // Map<String, String> headers = new LinkedHashMap<>();
    // headers.put("imei", "123123123");
    // headers.put("version", "1.0");
    // headers.put("token", "");
    // headers.put("regid", "123123123");
    // YHRequestFactory.getRequestManger().setHeaders(headers);
    // 缓存http
    // Cache cache = new Cache(new File(FileUtils.getSavePath(Constants.httpCachePath)), cacheSize);
    // cookie
    ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getInstance()));
    HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(60000L, TimeUnit.MILLISECONDS).readTimeout(60000L, TimeUnit.MILLISECONDS).writeTimeout(60000L, TimeUnit.MILLISECONDS).addInterceptor(// 日志拦截 是否显示返回数据
    new LoggerInterceptor("", true)).addInterceptor(// 统一请求头
    new HeaderInterceptor()).addNetworkInterceptor(new // 添加网络拦截器缓存用
    Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            if (NetWorkUtils.isConnectedByState(getInstance())) {
                // 有网 就1个小时可用 缓存有效时间
                int maxAge = 60 * 60;
                return response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).build();
            } else {
                // 没网 就1周可用 缓存有效时间
                int maxStale = 60 * 60 * 24 * 7;
                return response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).build();
            }
        }
    }).hostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }).cookieJar(cookieJar).sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).build();
    OkHttpUtils.initClient(okHttpClient);
}
Also used : ClearableCookieJar(com.franmontiel.persistentcookiejar.ClearableCookieJar) OkHttpClient(okhttp3.OkHttpClient) LoggerInterceptor(org.yh.library.okhttp.utils.LoggerInterceptor) PersistentCookieJar(com.franmontiel.persistentcookiejar.PersistentCookieJar) HeaderInterceptor(org.yh.library.okhttp.utils.HeaderInterceptor) SetCookieCache(com.franmontiel.persistentcookiejar.cache.SetCookieCache) Request(okhttp3.Request) SSLSession(javax.net.ssl.SSLSession) SharedPrefsCookiePersistor(com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor) HttpsUtils(org.yh.library.okhttp.https.HttpsUtils) IOException(java.io.IOException) HostnameVerifier(javax.net.ssl.HostnameVerifier) Response(okhttp3.Response)

Aggregations

Test (org.junit.Test)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 OkHttpClient (okhttp3.OkHttpClient)14 CookieManager (java.net.CookieManager)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 HttpCookie (java.net.HttpCookie)8 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 Request (okhttp3.Request)6 Response (okhttp3.Response)5 NonNull (android.support.annotation.NonNull)4 IOException (java.io.IOException)4 List (java.util.List)4 CookieHashSet (me.postaddict.instagram.scraper.cookie.CookieHashSet)4 DefaultCookieJar (me.postaddict.instagram.scraper.cookie.DefaultCookieJar)4 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)4 UserAgentInterceptor (me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor)4 Cookie (okhttp3.Cookie)4 JavaNetCookieJar (okhttp3.JavaNetCookieJar)4 ClearableCookieJar (com.franmontiel.persistentcookiejar.ClearableCookieJar)3