Package javax.net.ssl

Examples of javax.net.ssl.HttpsURLConnection


     * @throws GeneralSecurityException
     */
    private URLConnection getUrlConnection(URL url) throws IOException, GeneralSecurityException {
        final URLConnection orgcon = url.openConnection();
        if (orgcon instanceof HttpsURLConnection) {
            HttpsURLConnection con = (HttpsURLConnection) orgcon;
            con.setHostnameVerifier(new SimpleVerifier());
            con.setSSLSocketFactory(getSSLFactory());
        }
        return orgcon;
    }
View Full Code Here


    public HttpsTransport(TextWireFormat wireFormat, URI remoteUrl) throws MalformedURLException {
        super(wireFormat, remoteUrl);
    }

    protected synchronized HttpURLConnection createSendConnection() throws IOException {
        HttpsURLConnection conn = (HttpsURLConnection) getRemoteURL().openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        configureConnection(conn);
        conn.connect();
        return conn;
    }
View Full Code Here

        conn.connect();
        return conn;
    }

    protected synchronized HttpURLConnection createReceiveConnection() throws IOException {
        HttpsURLConnection conn = (HttpsURLConnection) getRemoteURL().openConnection();
        conn.setDoOutput(false);
        conn.setDoInput(true);
        conn.setRequestMethod("GET");
        configureConnection(conn);
        conn.connect();
        return conn;
    }
View Full Code Here

    // Get the size of a file from URL response header.
    public static Long getRemoteSize(String url) {
        Long remoteSize = (long) 0;
        HttpURLConnection httpConn = null;
        HttpsURLConnection httpsConn = null;
        try {
            URI uri = new URI(url);
            if (uri.getScheme().equalsIgnoreCase("http")) {
                httpConn = (HttpURLConnection) uri.toURL().openConnection();
                if (httpConn != null) {
                    String contentLength = httpConn.getHeaderField("content-length");
                    if (contentLength != null) {
                        remoteSize = Long.parseLong(contentLength);
                    }
                    httpConn.disconnect();
                }
            } else if (uri.getScheme().equalsIgnoreCase("https")) {
                httpsConn = (HttpsURLConnection) uri.toURL().openConnection();
                if (httpsConn != null) {
                    String contentLength = httpsConn.getHeaderField("content-length");
                    if (contentLength != null) {
                        remoteSize = Long.parseLong(contentLength);
                    }
                    httpsConn.disconnect();
                }
            }
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Invalid URL " + url);
        } catch (IOException e) {
View Full Code Here

        return;
    }

    public HttpsURLConnection openSecureConnection(URL url) throws Exception {
        // we assume the URL is https - if it is not, its an error so just let the cast throw exception
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        TrustManager[] trustManagers;
        SSLContext sslContext = SSLContext.getInstance(getSecureSocketProtocol());

        if (getTruststoreFile() == null) {
            // we are configured to not care about authenticating the server, just encrypt but don't worry about certificates
            trustManagers = new TrustManager[] { NO_OP_TRUST_MANAGER };
            connection.setHostnameVerifier(NO_OP_HOSTNAME_VERIFIER);
        } else {
            // We need to configure our SSL connection with the agent's truststore so we can authenticate the server.
            // First, create a KeyStore, but load it with our truststore entries.
            KeyStore keyStore = KeyStore.getInstance(getTruststoreType());
            keyStore.load(new FileInputStream(getTruststoreFile()), getTruststorePassword().toCharArray());
            // now create a truststore manager instance and initialize it with our KeyStore we created with all our truststore entries
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(getTruststoreAlgorithm());
            tmf.init(keyStore);
            trustManagers = tmf.getTrustManagers();
        }

        sslContext.init(null, trustManagers, null);
        connection.setSSLSocketFactory(sslContext.getSocketFactory());

        return connection;
    }
View Full Code Here

            return true;
        }
    };

    public static void main(String[] args) throws Exception {
        HttpsURLConnection conn = new SecureConnector("TLS").openSecureConnection(new URL(args[0]));
        java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
        java.io.InputStream input = new java.io.BufferedInputStream(conn.getInputStream(), 32768);
        byte[] buffer = new byte[32768];
        for (int bytesRead = input.read(buffer); bytesRead != -1; bytesRead = input.read(buffer)) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
View Full Code Here

        int responseCode = -1;
        int outVal = -1;
        if (url != null) {
            try {
                if (url.getProtocol().equals("https")) {
                    HttpsURLConnection htuc = (HttpsURLConnection)url.openConnection();
                    responseCode = htuc.getResponseCode();
                    htuc.disconnect();
                } else if (url.getProtocol().equals("http")) {
                    HttpURLConnection htuc = (HttpURLConnection)url.openConnection();
                    responseCode = htuc.getResponseCode();
                    htuc.disconnect();
                } else {
                    outVal = 1; // invalid protocol
                }
            } catch (IOException ioe) {}
            if (responseCode == HttpURLConnection.HTTP_OK) {
View Full Code Here

  @VisibleForTesting
  protected HttpURLConnection 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());
    }
    return conn;
  }
View Full Code Here

            connection.setInstanceFollowRedirects(false);
            connection.setUseCaches(false);

            if (connection instanceof HttpsURLConnection
                && hostnameVerifier != null) {
                HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
                httpsConnection.setHostnameVerifier(hostnameVerifier);
            }

            // Set the request headers
            if (result != null) {
                connection.setRequestProperty("Content-Type", serializerLocal.getMIMEType(result));
View Full Code Here

            verifier = CertificateHostnameVerifier.DEFAULT;
        }
       
        if (connection instanceof HttpsURLConnection) {
            // handle the expected case (javax.net.ssl)
            HttpsURLConnection conn = (HttpsURLConnection) connection;
            conn.setHostnameVerifier(verifier);
            conn.setSSLSocketFactory(socketFactory);
        } else {
            // handle the deprecated sun case and other possible hidden API's
            // that are similar to the Sun cases
            try {
                Method method = connection.getClass().getMethod("getHostnameVerifier");
View Full Code Here

TOP

Related Classes of javax.net.ssl.HttpsURLConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.