List of usage examples for com.squareup.okhttp OkHttpClient setHostnameVerifier
public OkHttpClient setHostnameVerifier(HostnameVerifier hostnameVerifier)
From source file:com.android.mms.service_alt.MmsHttpClient.java
License:Apache License
/** * Open an HTTP connection/*from w ww . ja v a2 s .co m*/ * * TODO: The following code is borrowed from android.net.Network.openConnection * Once that method supports proxy, we should use that instead * Also we should remove the associated HostResolver and ConnectionPool from * MmsNetworkManager * * @param url The URL to connect to * @param proxy The proxy to use * @return The opened HttpURLConnection * @throws MalformedURLException If URL is malformed */ private HttpURLConnection openConnection(URL url, final Proxy proxy) throws MalformedURLException { final String protocol = url.getProtocol(); OkHttpClient okHttpClient; if (protocol.equals("http")) { okHttpClient = new OkHttpClient(); okHttpClient.setFollowRedirects(false); okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1)); okHttpClient.setProxySelector(new ProxySelector() { @Override public List<Proxy> select(URI uri) { if (proxy != null) { return Arrays.asList(proxy); } else { return new ArrayList<Proxy>(); } } @Override public void connectFailed(URI uri, SocketAddress address, IOException failure) { } }); okHttpClient.setAuthenticator(new com.squareup.okhttp.Authenticator() { @Override public Request authenticate(Proxy proxy, Response response) throws IOException { return null; } @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { return null; } }); okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT)); okHttpClient.setConnectionPool(new ConnectionPool(3, 60000)); okHttpClient.setSocketFactory(SocketFactory.getDefault()); Internal.instance.setNetwork(okHttpClient, mHostResolver); if (proxy != null) { okHttpClient.setProxy(proxy); } return new HttpURLConnectionImpl(url, okHttpClient); } else if (protocol.equals("https")) { okHttpClient = new OkHttpClient(); okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1)); HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier(); okHttpClient.setHostnameVerifier(verifier); okHttpClient.setSslSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory()); okHttpClient.setProxySelector(new ProxySelector() { @Override public List<Proxy> select(URI uri) { return Arrays.asList(proxy); } @Override public void connectFailed(URI uri, SocketAddress address, IOException failure) { } }); okHttpClient.setAuthenticator(new com.squareup.okhttp.Authenticator() { @Override public Request authenticate(Proxy proxy, Response response) throws IOException { return null; } @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { return null; } }); okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT)); okHttpClient.setConnectionPool(new ConnectionPool(3, 60000)); Internal.instance.setNetwork(okHttpClient, mHostResolver); return new HttpsURLConnectionImpl(url, okHttpClient); } else { throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol); } }
From source file:com.brq.wallet.bitid.BitIdAuthenticator.java
License:Microsoft Reference Source License
private OkHttpClient getOkHttpClient() { OkHttpClient client = new OkHttpClient(); if (!enforceSslCorrectness) { //user explicitly agreed to not check certificates client.setSslSocketFactory(SslUtils.SSL_SOCKET_FACTORY_ACCEPT_ALL); client.setHostnameVerifier(SslUtils.HOST_NAME_VERIFIER_ACCEPT_ALL); }//from w ww.ja va 2 s . c om client.setConnectTimeout(Constants.SHORT_HTTP_TIMEOUT_MS, TimeUnit.MILLISECONDS); client.setReadTimeout(Constants.SHORT_HTTP_TIMEOUT_MS, TimeUnit.MILLISECONDS); return client; }
From source file:com.codemodlabs.coordinate.Token.java
License:Open Source License
private void getTokenFromServer(Callback callback) throws IOException { class GetTokenTask extends AsyncTask { @Override//www.ja v a2s . c om protected Object doInBackground(Object[] objects) { try { final String access_code = ((Callback) objects[0]).access_code; String url_string = "https://login.uber.com/oauth/token"; OkHttpClient client = new OkHttpClient(); // Ignore invalid SSL endpoints. client.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); RequestBody formBody = new FormEncodingBuilder().add("client_secret", client_secret) .add("client_id", client_id).add("grant_type", "authorization_code") .add("redirect_uri", redirect_url).add("code", access_code).build(); Request request = new Request.Builder().url(url_string).post(formBody).build(); Response response = client.newCall(request).execute(); String response_body = response.body().string(); Gson gson = new Gson(); token = gson.fromJson(response_body, Token.class); return token; } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object result) { super.onPostExecute(result); Token token = (Token) result; Intent resultIntent = new Intent(); resultIntent.putExtra("access_token", token.access_token); resultIntent.putExtra("expires_in", token.expires_in); resultIntent.putExtra("token_type", token.token_type); resultIntent.putExtra("refresh_token", token.refresh_token); resultIntent.putExtra("scope", token.scope); setResult(2, resultIntent); finish(); } } new GetTokenTask().execute(callback); }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxCatalogQuery.java
public static void main(String[] args) { OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override//w ww . j a v a2 s.c o m public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/catalog/v1/record/105041001281F200") .get().addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); System.out.println(body); // PrintWriter out = new PrintWriter("catalog.json"); // out.println(body); // out.flush(); // out.close(); JSONObject obj = new JSONObject(body); String available = obj.getJSONObject("properties").getString("available"); System.out.println(available); // JSONArray arr = obj.getJSONArray("posts"); // for (int i = 0; i < arr.length(); i++) { // String post_id = arr.getJSONObject(i).getString("post_id"); // ...... // } MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, "{ \n \"startDate\":null,\n \"endDate\": null,\n \"searchAreaWkt\":null,\n \"tagResults\": false,\n \"filters\": [\"identifier = '105041001281F200'\"],\n \"types\":[\"Acquisition\"]\n}"); Request search_request = new Request.Builder().url("https://alpha.geobigdata.io/catalog/v1/search") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); response = client.newCall(search_request).execute(); body = response.body().string(); System.out.println(body); // PrintWriter out = new PrintWriter("search_result.json"); // out.println(body); // out.flush(); // out.close(); mediaType = MediaType.parse("application/json"); request_body = RequestBody.create(mediaType, "{\n \"rootRecordId\": \"105041001281F200\",\n \"maxdepth\": 2,\n \"direction\": \"both\",\n \"labels\": []\n }"); request = new Request.Builder().url("https://alpha.geobigdata.io/catalog/v1/traverse") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); response = client.newCall(request).execute(); body = response.body().string(); System.out.println(body); PrintWriter out = new PrintWriter("traverse_result.json"); out.println(body); out.flush(); out.close(); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxCredentialManager.java
public static Properties validateUserToken(String token) { Properties token_info = new Properties(); OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override/*from w w w . j av a 2 s. c o m*/ public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/auth/v1/validate_token").get() .addHeader("content-type", "application/json").addHeader("authorization", "Bearer " + token) .build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); token_info = parseResponse(body); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } return token_info; }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxS3Credentials.java
public static Properties getS3Credentials(String auth_header) { Properties creds = new Properties(); OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override//from w ww .j a va 2s . co m public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/s3creds/v1/prefix").get() .addHeader("content-type", "application/json").addHeader("authorization", auth_header).build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); creds = parseResponse(body); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } return creds; }
From source file:com.frostwire.http.HttpClient.java
License:Open Source License
private static OkHttpClient buildDefaultClient() { OkHttpClient c = new OkHttpClient(); c.setFollowRedirects(true);//w ww . j a v a 2 s. c om c.setFollowSslRedirects(true); c.setConnectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); c.setReadTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); c.setWriteTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); c.setHostnameVerifier(buildHostnameVerifier()); c.setSslSocketFactory(buildSSLSocketFactory()); c.interceptors().add(new GzipInterceptor()); return c; }
From source file:com.groupon.odo.bmp.BrowserMobProxyHandler.java
License:Apache License
/** * Returns a OkHttpClient that ignores SSL cert errors * @return/*from w w w .j a v a 2 s .com*/ */ private static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); // Create an ssl socket factory with our all-trusting manager final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setSslSocketFactory(sslSocketFactory); okHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); return okHttpClient; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.gst.infrastructure.hooks.processor.ProcessorHelper.java
License:Apache License
@SuppressWarnings("null") public static OkHttpClient configureClient(final OkHttpClient client) { final TrustManager[] certs = new TrustManager[] { new X509TrustManager() { @Override/*from w w w . j a va2 s. com*/ public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { } @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { } } }; SSLContext ctx = null; try { ctx = SSLContext.getInstance("TLS"); ctx.init(null, certs, new SecureRandom()); } catch (final java.security.GeneralSecurityException ex) { } try { final HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(final String hostname, final SSLSession session) { return true; } }; client.setHostnameVerifier(hostnameVerifier); client.setSslSocketFactory(ctx.getSocketFactory()); } catch (final Exception e) { } return client; }
From source file:com.mifos.services.API.java
private OkHttpClient getUnsafeOkHttpClient() { try {/*from w ww . j av a 2 s .c om*/ // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); // Create an ssl socket factory with our all-trusting manager final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setSslSocketFactory(sslSocketFactory); okHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); return okHttpClient; } catch (Exception e) { throw new RuntimeException(e); } }