Search in sources :

Example 66 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project android_frameworks_base by crdroidandroid.

the class TestUtils method assertUrlConnectionSucceeds.

public static void assertUrlConnectionSucceeds(SSLContext context, String host, int port) throws Exception {
    URL url = new URL("https://" + host + ":" + port);
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setSSLSocketFactory(context.getSocketFactory());
    connection.getInputStream();
}
Also used : URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 67 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project dropbox-sdk-java by dropbox.

the class StandardHttpRequestor method prepRequest.

private HttpURLConnection prepRequest(String url, Iterable<Header> headers) throws IOException {
    URL urlObject = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection(config.getProxy());
    conn.setConnectTimeout((int) config.getConnectTimeoutMillis());
    conn.setReadTimeout((int) config.getReadTimeoutMillis());
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    // instead of HttpsURLConnection. So we have to check here.
    if (conn instanceof HttpsURLConnection) {
        SSLConfig.apply((HttpsURLConnection) conn);
        configureConnection((HttpsURLConnection) conn);
    } else {
        logCertificatePinningWarning();
    }
    configure(conn);
    for (Header header : headers) {
        conn.addRequestProperty(header.getKey(), header.getValue());
    }
    return conn;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 68 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project bnd by bndtools.

the class HttpsUtil method disableServerVerification.

static void disableServerVerification(URLConnection connection) throws GeneralSecurityException {
    if (!(connection instanceof HttpsURLConnection))
        return;
    HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }
    } };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    httpsConnection.setSSLSocketFactory(sslSocketFactory);
    HostnameVerifier trustAnyHost = new HostnameVerifier() {

        public boolean verify(String string, SSLSession session) {
            return true;
        }
    };
    httpsConnection.setHostnameVerifier(trustAnyHost);
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) X509Certificate(java.security.cert.X509Certificate) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 69 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project bnd by bndtools.

the class HttpsVerification method handle.

/**
	 * Ensure Https verification is disabled or matches given certificates
	 */
public void handle(URLConnection connection) throws Exception {
    if (connection instanceof HttpsURLConnection && matches(connection)) {
        HttpsURLConnection https = (HttpsURLConnection) connection;
        init();
        https.setSSLSocketFactory(factory);
        https.setHostnameVerifier(verifier);
    }
}
Also used : HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 70 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project bnd by bndtools.

the class BndAuthentication method handle.

public void handle(URLConnection connection) throws Exception {
    if (!(connection instanceof HttpURLConnection) || !matches(connection))
        return;
    if (!(connection instanceof HttpsURLConnection))
        logger.debug("bnd authentication should only be used with https: {}", connection.getURL());
    init();
    // Build up Authorization header
    StringBuilder sb = new StringBuilder(identity);
    // Get the date header, set it if not set
    String dateHeader = connection.getRequestProperty("Date");
    if (dateHeader == null) {
        synchronized (httpFormat) {
            dateHeader = httpFormat.format(new Date());
        }
        connection.setRequestProperty("Date", dateHeader);
    }
    // Ok, calculate the signature
    Signature hmac = Signature.getInstance("SHA1withRSA");
    hmac.initSign(privateKey);
    // never non-ascii
    hmac.update(dateHeader.getBytes());
    // Finish the header
    sb.append(Base64.encodeBase64(hmac.sign()));
    connection.setRequestProperty(X_A_QUTE_AUTHORIZATION, sb.toString());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Signature(java.security.Signature) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Date(java.util.Date)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)522 URL (java.net.URL)310 IOException (java.io.IOException)177 HttpURLConnection (java.net.HttpURLConnection)128 InputStreamReader (java.io.InputStreamReader)93 InputStream (java.io.InputStream)89 Test (org.junit.Test)83 BufferedReader (java.io.BufferedReader)78 SSLContext (javax.net.ssl.SSLContext)70 OutputStream (java.io.OutputStream)54 HostnameVerifier (javax.net.ssl.HostnameVerifier)50 MalformedURLException (java.net.MalformedURLException)48 URLConnection (java.net.URLConnection)47 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 HashMap (java.util.HashMap)34 DataOutputStream (java.io.DataOutputStream)32 KeyManagementException (java.security.KeyManagementException)32 JSONObject (org.json.JSONObject)29