Search in sources :

Example 81 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project smarthome by eclipse.

the class HttpTransportImpl method checkConnection.

@Override
public int checkConnection(String testRequest) {
    try {
        HttpsURLConnection connection = getConnection(testRequest, connectTimeout, readTimeout);
        if (connection != null) {
            connection.connect();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                if (IOUtils.toString(connection.getInputStream()).contains("Authentication failed")) {
                    return ConnectionManager.AUTHENTIFICATION_PROBLEM;
                }
            }
            connection.disconnect();
            return connection.getResponseCode();
        } else {
            return ConnectionManager.GENERAL_EXCEPTION;
        }
    } catch (SocketTimeoutException e) {
        return ConnectionManager.SOCKET_TIMEOUT_EXCEPTION;
    } catch (java.net.ConnectException e) {
        return ConnectionManager.CONNECTION_EXCEPTION;
    } catch (MalformedURLException e) {
        return ConnectionManager.MALFORMED_URL_EXCEPTION;
    } catch (java.net.UnknownHostException e) {
        return ConnectionManager.UNKNOWN_HOST_EXCEPTION;
    } catch (IOException e) {
        return ConnectionManager.GENERAL_EXCEPTION;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 82 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project smarthome by eclipse.

the class HttpTransportImpl method getConnection.

private HttpsURLConnection getConnection(String request, int connectTimeout, int readTimeout) throws IOException {
    if (StringUtils.isNotBlank(request)) {
        request = fixRequest(request);
        URL url = new URL(this.uri + request);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        if (connection != null) {
            connection.setConnectTimeout(connectTimeout);
            connection.setReadTimeout(readTimeout);
            if (sslSocketFactory != null) {
                connection.setSSLSocketFactory(sslSocketFactory);
            }
            if (hostnameVerifier != null) {
                connection.setHostnameVerifier(hostnameVerifier);
            }
        }
        return connection;
    }
    return null;
}
Also used : URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 83 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project samourai-wallet-android by Samourai-Wallet.

the class SSLVerifierUtil method certificatePinned.

public int certificatePinned() {
    // DER encoded public key:
    // samourai.io
    // 30820222300d06092a864886f70d01010105000382020f003082020a0282020100b1ea258ba61430ba05233fb9c5bcabfee7f157a321bac74236660b46b96537832986c3f6ceb4ff2d6456d85bf7599cad36df493a3f91472bdc6ab6098bd77a0089619c07421eb0f5bdd70133c8f7b671a4624e0e129b5a3e13a40fbfe3bf3d1c88c8fc4aa747ec11f245568754f3305010ccab53ce55bf7794749d8a7c12bdd4497f7bbf60c380d7a9303f016cd3e52a769f51a1d14e6c7348233ab32dc00b2a4abdb6c9299fe120b71a9b385ec91f0b05e99edb3a294de4857e2067faeea9d6c2425dae3b1d76d5c53cdfa9c4b17281efd8bb9d3e6b7a2e99e65c426b638ba98771484e1cd094c37fdcbd2fae40794ea43ffc66df753cb73498f97eb0b37149858411b61000ca5972ba9b1836862c4d05c54b4aef0521a6a2376e03cdc2cd92b7ed9509fc5d7683da7af2d379cf4eba8da6fa37147870bb13495413c6a5fe06e0605ed24b413fb845e38d9ff76256e11fdcd30eceea53161cdea6c93ce84836f48bee6923a877ec762ca1856dcd8c7bd1b8d68467a8ea1916503e9e08dd8111b9ce0ca76a93f68f2692b9c6a43659ee1921550f7d7a30ebf3a0e4081e4112ffc5298cf1956093400690c81af4a1defaf2ea67c3b4b5abf76a4f915d6c2ca6dd911be6705371f2de85dcbe28e85a21bce2979864cc015fa670250d47abe5048d479482d015e26d1f9d81f365115a0cdfb7ddb875ddad030f3cd50f1c61f31a230203010001
    // SHA-1 hash of DER encoded public key byte array
    String[] pins = new String[] { "90abe5561984eb7647128b6efef9189d488fd587" };
    URL url = null;
    try {
        url = new URL(WebUtil.VALIDATE_SSL_URL);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpsURLConnection connection = null;
    try {
        connection = PinningHelper.getPinnedHttpsURLConnection(context, pins, url);
    } catch (IOException e) {
        e.printStackTrace();
        return STATUS_POTENTIAL_SERVER_DOWN;
    }
    try {
        byte[] data = new byte[4096];
        connection.getInputStream().read(data);
        return STATUS_PINNING_SUCCESS;
    } catch (IOException e) {
        e.printStackTrace();
        if (e instanceof SSLHandshakeException) {
            return STATUS_PINNING_FAIL;
        } else {
            return STATUS_POTENTIAL_SERVER_DOWN;
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 84 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project Angelia-core by Maxopoly.

the class AuthenticationHandler method sendPost.

private String sendPost(String content, String url, Logger logger) throws IOException {
    try {
        byte[] contentBytes = content.getBytes("UTF-8");
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept-Charset", "UTF-8");
        con.setRequestProperty("Content-Length", Integer.toString(contentBytes.length));
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(contentBytes, 0, contentBytes.length);
        wr.close();
        int responseCode = con.getResponseCode();
        if ((responseCode / 100) != 2) {
            // we want a 200 something response code
            throw new IOException("POST to " + url + " returned bad response code " + responseCode);
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    } catch (Exception e) {
        logger.error("Exception occured", e);
        throw new IOException("Failed to send POST to " + url + " : " + e.getClass());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) IOException(java.io.IOException)

Example 85 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project synergic-developing by zeemood.

the class WebUtils method getConnection.

private static HttpURLConnection getConnection(URL url, String method, String ctype, Proxy proxy) throws IOException {
    HttpURLConnection conn = null;
    if ("https".equals(url.getProtocol())) {
        HttpsURLConnection connHttps = null;
        if (proxy != null) {
            connHttps = (HttpsURLConnection) url.openConnection(proxy);
        } else {
            connHttps = (HttpsURLConnection) url.openConnection();
        }
        connHttps.setSSLSocketFactory(socketFactory);
        connHttps.setHostnameVerifier(verifier);
        conn = connHttps;
    } else {
        conn = null;
        if (proxy != null) {
            conn = (HttpURLConnection) url.openConnection(proxy);
        } else {
            conn = (HttpURLConnection) url.openConnection();
        }
    }
    conn.setRequestMethod(method);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");
    conn.setRequestProperty("User-Agent", "aop-sdk-java");
    conn.setRequestProperty("Content-Type", ctype);
    return conn;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

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