Search in sources :

Example 61 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project collect by opendatakit.

the class SNITest method urlConnectionSupportsSNI.

@Test
public void urlConnectionSupportsSNI() throws IOException {
    HttpsURLConnection conn = (HttpsURLConnection) SNI_URI.toURL().openConnection();
    assertHttpSuccess(conn.getResponseCode());
    assertPageContent(conn.getInputStream());
}
Also used : HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 62 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project java-google-speech-api by goxr3plus.

the class GSpeechDuplex method openHttpsPostConnection.

/**
 * Opens a chunked HTTPS post connection and returns a Scanner with incoming data from Google Server Used for to get UPStream Chunked HTTPS
 * ensures unlimited file size.
 *
 * @param urlStr
 *            The String for the URL
 * @param data
 *            The data you want to send the server
 * @param sampleRate
 *            The sample rate of the flac file.
 * @return A Scanner to access the server response. (Probably will never be used)
 */
private Scanner openHttpsPostConnection(String urlStr, byte[][] data, int sampleRate) {
    byte[][] mextrad = data;
    int resCode = -1;
    OutputStream out = null;
    // int http_status;
    try {
        URL url = new URL(urlStr);
        HttpsURLConnection httpConn = getHttpsURLConnection(sampleRate, url);
        // this opens a connection, then sends POST & headers.
        out = httpConn.getOutputStream();
        // Note : if the audio is more than 15 seconds
        // dont write it to UrlConnInputStream all in one block as this sample does.
        // Rather, segment the byteArray and on intermittently, sleeping thread
        // supply bytes to the urlConn Stream at a rate that approaches
        // the bitrate ( =30K per sec. in this instance ).
        System.out.println("Starting to write");
        for (byte[] dataArray : mextrad) {
            // one big block supplied instantly to the underlying chunker wont work for duration > 15 s.
            out.write(dataArray);
            try {
                // Delays the Audio so Google thinks its a mic.
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        out.write(FINAL_CHUNK);
        System.out.println("IO WRITE DONE");
        // do you need the trailer?
        // NOW you can look at the status.
        resCode = httpConn.getResponseCode();
        if (resCode / 100 != 2) {
            System.out.println("ERROR");
        }
        if (resCode == HttpsURLConnection.HTTP_OK) {
            return new Scanner(httpConn.getInputStream(), "UTF-8");
        } else {
            System.out.println("HELP: " + resCode);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Scanner(java.util.Scanner) MalformedURLException(java.net.MalformedURLException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 63 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project Gargoyle by callakrsos.

the class RequestUtil method requestSSL.

public static <T> T requestSSL(URL url, ResponseHandler<T> response, boolean autoClose) throws Exception {
    // SSLContext ctx = SSLContext.getInstance("TLS");
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    InputStream is = null;
    T result = null;
    try {
        conn.setDefaultUseCaches(true);
        conn.setUseCaches(true);
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
        conn.setRequestProperty("Accept-Encoding", "UTF-8");
        // conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Accept", "text/html");
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Accept-Encoding", "UTF-8");
        conn.setRequestProperty("Accept-Language", "KR");
        // conn.setRequestProperty("Cache-Control", "no-store");
        // conn.setRequestProperty("Pragma", "no-cache");
        conn.setHostnameVerifier(hostnameVerifier);
        conn.setConnectTimeout(6000);
        conn.connect();
        is = conn.getInputStream();
        String contentEncoding = conn.getContentEncoding();
        LOGGER.debug("code : [{}] [{}] URL : {} ,  ", conn.getResponseCode(), contentEncoding, url.toString());
        response.setContentEncoding(contentEncoding);
        response.setResponseCode(conn.getResponseCode());
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        response.setHeaderFields(headerFields);
        result = response.apply(is, conn.getResponseCode());
    } finally {
        if (autoClose) {
            if (is != null)
                is.close();
            if (conn != null)
                conn.disconnect();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 64 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project oxTrust by GluuFederation.

the class StatusCheckerTimer method setCertificateExpiryAttributes.

private void setCertificateExpiryAttributes(GluuAppliance appliance) {
    try {
        URL destinationURL = new URL(appConfiguration.getApplianceUrl());
        HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
        conn.connect();
        Certificate[] certs = conn.getServerCertificates();
        if (certs.length > 0) {
            if (certs[0] instanceof X509Certificate) {
                X509Certificate x509Certificate = (X509Certificate) certs[0];
                Date expirationDate = x509Certificate.getNotAfter();
                long expiresAfter = TimeUnit.MILLISECONDS.toDays(expirationDate.getTime() - new Date().getTime());
                appliance.setSslExpiry(toIntString(expiresAfter));
            }
        }
    } catch (IOException e) {
        log.error("Can not download ssl certificate", e);
    }
}
Also used : IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 65 with HttpsURLConnection

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

the class TestUtils method assertUrlConnectionFails.

public static void assertUrlConnectionFails(SSLContext context, String host, int port) throws Exception {
    URL url = new URL("https://" + host + ":" + port);
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setSSLSocketFactory(context.getSocketFactory());
    try {
        connection.getInputStream();
        fail("Connection to " + host + ":" + port + " expected to fail");
    } catch (SSLHandshakeException expected) {
    // ignored.
    }
}
Also used : URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

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