public GitHub initGithub() { String tmpDirPath = System.getProperty("java.io.tmpdir"); File cacheDirectoryParent = new File(tmpDirPath); File cacheDirectory = new File(cacheDirectoryParent, "okhttpCache"); if (!cacheDirectory.exists()) { cacheDirectory.mkdir(); } Cache cache = new Cache(cacheDirectory, 100 * 1024 * 1024); try { return GitHubBuilder.fromCredentials() .withRateLimitHandler(RateLimitHandler.WAIT) .withAbuseLimitHandler(AbuseLimitHandler.WAIT) .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache)))) .build(); } catch (IOException e) { throw new RuntimeException(e); } }
static byte[] fetch(URL url) throws IOException { InputStream in = null; try { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = new OkUrlFactory(client).open(url); ByteArrayOutputStream out = new ByteArrayOutputStream(); in = conn.getInputStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } return out.toByteArray(); } finally { if (in != null) { in.close(); } } }
static String fetchPlainText(URL url) throws IOException { InputStream in = null; try { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = new OkUrlFactory(client).open(url); conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); conn.setReadTimeout(DEFAULT_READ_TIMEOUT); in = conn.getInputStream(); return readFullyPlainText(in); } finally { if (in != null) { in.close(); } } }
/** * Initializes the global URLStreamHandlerFactory. * <p> * This method is invoked by {@link #init(boolean, boolean)}. */ public static void initProtocols(final SSLSocketFactory sslSocketFactory) { // Configure URL protocol handlers final PlatformStreamHandlerFactory factory = PlatformStreamHandlerFactory.getInstance(); URL.setURLStreamHandlerFactory(factory); final OkHttpClient okHttpClient = new OkHttpClient(); final ArrayList<Protocol> protocolList = new ArrayList<>(2); protocolList.add(Protocol.HTTP_1_1); protocolList.add(Protocol.HTTP_2); okHttpClient.setProtocols(protocolList); okHttpClient.setConnectTimeout(100, TimeUnit.SECONDS); // HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory); okHttpClient.setSslSocketFactory(sslSocketFactory); okHttpClient.setFollowRedirects(false); okHttpClient.setFollowSslRedirects(false); factory.addFactory(new OkUrlFactory(okHttpClient)); factory.addFactory(new LocalStreamHandlerFactory()); }
@Override protected GapiOkHttpRequest buildRequest(String method, String url) throws IOException { Preconditions.checkArgument(supportsMethod(method), "HTTP method %s not supported", method); // connection with proxy settings URL connUrl = new URL(url); OkHttpClient client = new OkHttpClient(); OkUrlFactory factory = new OkUrlFactory(client); SSLContext sslContext; try { sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, null, null); } catch (GeneralSecurityException e) { throw new AssertionError(); // The system has no TLS. Just give up. } client.setSslSocketFactory(sslContext.getSocketFactory()); if(proxy != null) client.setProxy(proxy); URLConnection conn = factory.open(connUrl); HttpURLConnection connection = (HttpURLConnection) conn; connection.setRequestMethod(method); return new GapiOkHttpRequest(connection); }
@Override protected HttpURLConnection createConnection(URL url) throws IOException { OkHttpClient client = new OkHttpClient(); OkUrlFactory factory = new OkUrlFactory(client); SSLContext sslContext; try { TrustManager[] trustAllCerts = new TrustManager[] { new GdgTrustManager(App.getInstance().getApplicationContext()) }; sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); } catch (GeneralSecurityException e) { throw new AssertionError(); // The system has no TLS. Just give up. } client.setSslSocketFactory(sslContext.getSocketFactory()); return factory.open(url); }
public OkHttpStack(OkUrlFactory okUrlFactory) { this.mUrlRewriter = null; this.mSslSocketFactory = null; if (okUrlFactory == null) { throw new NullPointerException("Client must not be null."); } this.okUrlFactory = okUrlFactory; }
public OkHttpStack(OkHttpClient client) { if (client == null) { throw new NullPointerException("Client must not be null."); } client.interceptors().add(new GzipRequestInterceptor()); mFactory = new OkUrlFactory(client); }
@Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { ParcelFileDescriptor[] pipe = null; String url = uri.getPath(); try { String decodedUrl = URLDecoder.decode(url.replaceFirst("/", ""), "UTF-8"); pipe = ParcelFileDescriptor.createPipe(); OkHttpClient httpClient = PopcornApplication.getHttpClient(); OkUrlFactory factory = new OkUrlFactory(httpClient); HttpURLConnection connection = factory.open(new URL(decodedUrl)); new TransferThread(connection.getInputStream(), new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])) .start(); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Exception opening pipe", e); throw new FileNotFoundException("Could not open pipe for: " + uri.toString()); } return (pipe[0]); }
public static HttpURLConnection getHttpURLConnection(final URL url, final Cache cache, final SSLSocketFactory sslSocketFactory) { OkHttpClient client = new OkHttpClient(); if (cache != null) { client.setCache(cache); } if (sslSocketFactory != null) { client.setSslSocketFactory(sslSocketFactory); } HttpURLConnection connection = new OkUrlFactory(client).open(url); connection.setRequestProperty("User-Agent", MapboxUtils.getUserAgent()); return connection; }
public OkHttpStack(OkHttpClient client) { if (client == null) { throw new NullPointerException("client cannot be null!"); } okHttpClient = client; this.client = client; this.urlFactory = new OkUrlFactory(this.client); }
protected HttpURLConnection createConnection(String url) throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); //okHttpClient.setSslSocketFactory(HttpRequest.getTrustedFactory()); //okHttpClient.setHostnameVerifier(HttpRequest.getTrustedVerifier()); //okHttpClient.networkInterceptors().add(new StethoInterceptor()); HttpURLConnection conn = new OkUrlFactory(okHttpClient).open(new URL(url)); return conn; }
public static HttpURLConnection getHttpURLConnection(final URL url, final Cache cache, final SSLSocketFactory sslSocketFactory) { OkHttpClient client = new OkHttpClient(); if (cache != null) { client.setCache(cache); } if (sslSocketFactory != null) { client.setSslSocketFactory(sslSocketFactory); } HttpURLConnection connection = new OkUrlFactory(client).open(url); connection.setRequestProperty("User-Agent", MapboxConstants.USER_AGENT); return connection; }
private OkUrlFactory generateDefaultOkUrlFactory() { OkHttpClient client = new com.squareup.okhttp.OkHttpClient(); try { Cache responseCache = new Cache(baseContext.getCacheDir(), SIZE_OF_CACHE); client.setCache(responseCache); } catch (Exception e) { // Log.d(TAG, "Unable to set http cache", e); } client.setConnectTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS); client.setReadTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS); return new OkUrlFactory(client); }
public OkHttpStack(OkHttpClient client) { if (client == null) { throw new NullPointerException("Client must not be null."); } try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, null, null); client.setSslSocketFactory(sslContext.getSocketFactory()); } catch (Exception e) { throw new AssertionError(); // The system has no TLS. Just give up. } mFactory = new OkUrlFactory(client); }
public OkHttpStack() { this(new OkUrlFactory(new OkHttpClient())); }
public HttpURLConnection create(URL url) throws IOException { return new OkUrlFactory(client).open(url); }
@Override protected HttpURLConnection createConnection(URL url) throws IOException { OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient); return okUrlFactory.open(url); }
public static OkUrlFactory getOkUrlFactory() { return urlFactory; }
public OkHttpStack(OkHttpClient client) { if (client == null) { throw new NullPointerException("Client must not be null."); } mFactory = new OkUrlFactory(client); }
public OkHttpStack(OkUrlFactory okUrlFactory) { if (okUrlFactory == null) { throw new NullPointerException("Client must not be null."); } this.okUrlFactory = okUrlFactory; }
private OkUrlFactory getUrlFactory() { return new OkUrlFactory(getOkHttpClient()); }
public OkHttpConnector(OkUrlFactory urlFactory) { this.urlFactory = urlFactory; }
public OkUrlConnectionClient(OkHttpClient okhttpclient) { mURLFactory = new OkUrlFactory(okhttpclient); }
private static void apacheAlikeApiSample() throws Exception { System.out.println("::: It's a apache client alike api usage."); // ::: It's a apache client alike api usage. // Create a apache kind of http client which you can share in whole application OkApacheClient client = new OkApacheClient(); client.impl().setHostnameVerifier(UTILS.createInsecureHostnameVerifier()); client.impl().setSslSocketFactory(UTILS.createInsecureSslSocketFactory()); // Config the preferred protocol for special host you prefer to invoke with special protocol. client.configPreferredHostProtocol("118.186.217.31", Protocol.SPDY_3); // http://118.186.217.31 hosts with a spdy style http service, we create a get request for it HttpGet request = new HttpGet(new URL("http://118.186.217.31/").toURI()); // HttpGet request = new HttpGet(new URL("https://www.cloudflare.com/").toURI()); // then run it. HttpResponse response = client.execute(request); // should OUTPUT: // HTTP/1.1 200 OK [OkHttp-Selected-Protocol: spdy/3.1, server: nginx/1.5.11, date: \ // Thu, 12 Jun 2014 09:09:34 GMT, content-type: text/html; charset=UTF-8, alternate-protocol:\ // 443:npn-spdy/3, OkHttp-Sent-Millis: 1402564173672, OkHttp-Received-Millis: 1402564173708]\ // HTTP/1.1 200 OK [OkHttp-Selected-Protocol: spdy/3.1, server: cloudflare-nginx, date: \ // Wed, 18 Jun 2014 10:35:44 GMT, content-type: text/html; charset=UTF-8, set-cookie: \ // __cfduid=dec44ce8ac515d613f7490568f39612f21403087744468; expires=Mon, 23-Dec-2019 23:50:00 GMT; \ // path=/; domain=.cloudflare.com; HttpOnly, expires: Wed, 18 Jun 2014 14:35:44 GMT, cache-control: \ // public, max-age=14400, pragma: no-cache, x-frame-options: DENY, vary: Accept-Encoding, \ // strict-transport-security: max-age=31536000, cf-cache-status: HIT, cf-ray: 13c6d782e0ac0d31-LAX, \ // OkHttp-Sent-Millis: 1403087740701, OkHttp-Received-Millis: 1403087741183] System.out.println(response); String actual = EntityUtils.toString(response.getEntity(), UTF_8); System.out.println(actual); System.out.println(":::\n"); System.out.println("::: It's a Url connection alike api usage."); // ::: It's a Url connection alike api usage // firstly, we create a okhttp lib client for further using OkHttpClient ok = new OkHttpClient(); ok.setHostnameVerifier(UTILS.createInsecureHostnameVerifier()); ok.setSslSocketFactory(UTILS.createInsecureSslSocketFactory()); // we config a host with a prefered portocol kind // actally http://118.186.217.31 hosts with a spdy style http service // ok.configPreferredHostProtocol("118.186.217.31", Protocol.SPDY_3); // with the customized client, create a url factory OkUrlFactory factory = new OkUrlFactory(ok); // open an connection from a URL HttpURLConnection connection = factory.open(new URL("https://118.186.217.31")); connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8)); for (String line = null; (line = reader.readLine()) != null;) System.out.println(line); reader.close(); connection.disconnect(); System.out.println(":::\n"); }
protected HttpURLConnection createConnection(URL url) throws IOException { return new OkUrlFactory(client).open(url); }
public OkHttpStack(OkHttpClient client) { if (client == null) { throw new NullPointerException("OkHttpClient must not be null."); } mFactory = new OkUrlFactory(client); }