Search in sources :

Example 1 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project UltimateAndroid by cymcsg.

the class HttpsUtils method sendWithSSlSocket.

/**
     * @deprecated
     */
public static void sendWithSSlSocket(String urlLink) {
    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    URL url = null;
    try {
        url = new URL(urlLink);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(sslsocketfactory);
        InputStream inputstream = conn.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String string = null;
        while ((string = bufferedreader.readLine()) != null) {
            System.out.println("Received " + string);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
    }
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project buck by facebook.

the class HttpDownloader method fetch.

@Override
public boolean fetch(BuckEventBus eventBus, URI uri, Optional<PasswordAuthentication> authentication, Path output) throws IOException {
    if (!("https".equals(uri.getScheme()) || "http".equals(uri.getScheme()))) {
        return false;
    }
    DownloadEvent.Started started = DownloadEvent.started(uri);
    eventBus.post(started);
    try {
        HttpURLConnection connection = createConnection(uri);
        if (authentication.isPresent()) {
            if ("https".equals(uri.getScheme()) && connection instanceof HttpsURLConnection) {
                PasswordAuthentication p = authentication.get();
                String authStr = p.getUserName() + ":" + new String(p.getPassword());
                String authEncoded = BaseEncoding.base64().encode(authStr.getBytes(StandardCharsets.UTF_8));
                connection.addRequestProperty("Authorization", "Basic " + authEncoded);
            } else {
                LOG.info("Refusing to send basic authentication over plain http.");
                return false;
            }
        }
        if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
            LOG.info("Unable to download %s: %s", uri, connection.getResponseMessage());
            return false;
        }
        long contentLength = connection.getContentLengthLong();
        try (InputStream is = new BufferedInputStream(connection.getInputStream());
            OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) {
            long read = 0;
            while (true) {
                int r = is.read();
                read++;
                if (r == -1) {
                    break;
                }
                if (read % PROGRESS_REPORT_EVERY_N_BYTES == 0) {
                    eventBus.post(new DownloadProgressEvent(uri, contentLength, read));
                }
                os.write(r);
            }
        }
        return true;
    } finally {
        eventBus.post(DownloadEvent.finished(started));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 3 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project jersey by jersey.

the class SslHttpUrlConnectorTest method testSSLWithCustomSocketFactory.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithCustomSocketFactory() throws Exception {
    final SSLContext sslContext = getSslContext();
    final CustomSSLSocketFactory socketFactory = new CustomSSLSocketFactory(sslContext);
    final ClientConfig cc = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {

        @Override
        public HttpURLConnection getConnection(final URL url) throws IOException {
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setSSLSocketFactory(socketFactory);
            return connection;
        }
    }));
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).register(HttpAuthenticationFeature.basic("user", "password")).register(LoggingFeature.class).build();
    final Response response = client.target(Server.BASE_URI).path("/").request().get();
    assertEquals(200, response.getStatus());
    assertTrue(socketFactory.isVisited());
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 4 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project hadoop by apache.

the class Fetcher method openConnection.

@VisibleForTesting
protected synchronized void openConnection(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    if (sslShuffle) {
        HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
        try {
            httpsConn.setSSLSocketFactory(sslFactory.createSSLSocketFactory());
        } catch (GeneralSecurityException ex) {
            throw new IOException(ex);
        }
        httpsConn.setHostnameVerifier(sslFactory.getHostnameVerifier());
    }
    connection = conn;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project hadoop by apache.

the class TimelineConnector method initSslConnConfigurator.

private static ConnectionConfigurator initSslConnConfigurator(final int timeout, SSLFactory sslFactory) throws IOException, GeneralSecurityException {
    final SSLSocketFactory sf;
    final HostnameVerifier hv;
    sf = sslFactory.createSSLSocketFactory();
    hv = sslFactory.getHostnameVerifier();
    return new ConnectionConfigurator() {

        @Override
        public HttpURLConnection configure(HttpURLConnection conn) throws IOException {
            if (conn instanceof HttpsURLConnection) {
                HttpsURLConnection c = (HttpsURLConnection) conn;
                c.setSSLSocketFactory(sf);
                c.setHostnameVerifier(hv);
            }
            setTimeouts(conn, timeout);
            return conn;
        }
    };
}
Also used : ConnectionConfigurator(org.apache.hadoop.security.authentication.client.ConnectionConfigurator) HttpURLConnection(java.net.HttpURLConnection) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)517 URL (java.net.URL)307 IOException (java.io.IOException)173 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)69 OutputStream (java.io.OutputStream)52 HostnameVerifier (javax.net.ssl.HostnameVerifier)49 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 MalformedURLException (java.net.MalformedURLException)46 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)46 URLConnection (java.net.URLConnection)45 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)36 HashMap (java.util.HashMap)34 DataOutputStream (java.io.DataOutputStream)32 KeyManagementException (java.security.KeyManagementException)31 JSONObject (org.json.JSONObject)29