Example usage for org.apache.http.conn.ssl SSLSocketFactory setHostnameVerifier

List of usage examples for org.apache.http.conn.ssl SSLSocketFactory setHostnameVerifier

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLSocketFactory setHostnameVerifier.

Prototype

public void setHostnameVerifier(final X509HostnameVerifier hostnameVerifier) 

Source Link

Usage

From source file:at.diamonddogs.net.WebClientDefaultHttpClient.java

/**
 * Default {@link WebClient} constructor
 * //from   w  w  w. j  a va2 s.c o  m
 * @param context
 *            a {@link Context} object
 */
public WebClientDefaultHttpClient(Context context) {
    super(context);
    SSLSocketFactory sslSocketFactory = SSLHelper.getInstance().SSL_FACTORY_APACHE;
    if (sslSocketFactory != null) {
        sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sslSocketFactory, 443));
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        httpClient = new DefaultHttpClient(ccm, params);
    } else {
        httpClient = new DefaultHttpClient();
    }
    httpClient.setHttpRequestRetryHandler(this);
    if (followProtocolRedirect) {
        httpClient.setRedirectHandler(this);
    }
}

From source file:com.codingPower.framework.worker.FileNetWorker.java

/**
 * ?httpClient// ww  w  .j a v a 2s  .co  m
 * @return
 */
protected HttpClient getHttpClient() {

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }

}

From source file:gov.nrel.bacnet.consumer.DatabusSender.java

public DefaultHttpClient createSecureOne(PoolingClientConnectionManager mgr) {
    try {//from w w w  .  j av  a 2s.c o  m
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String hostname, SSLSession session) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void verify(String arg0, SSLSocket arg1) throws IOException {
                // TODO Auto-generated method stub

            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        SchemeRegistry sr = mgr.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, port));
        return new DefaultHttpClient(mgr);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.damdi.fitness.activity.settings.sync.RestClient.java

/**
 * Creates a rest client./*from   w ww  . j  a v  a2 s  .c  om*/
 * 
 * @param hostname
 *            The host name of the server
 * @param port
 *            The TCP port of the server that should be addressed
 * @param scheme
 *            The used protocol scheme
 * @param versionCode
 *            The version of the app (used for user agent)
 * 
 */
public RestClient(final String hostname, final int port, final String scheme, final int versionCode) {
    final StringBuilder uri = new StringBuilder(scheme);
    uri.append("://");
    uri.append(hostname);
    if (port > 0) {
        uri.append(":");
        uri.append(port);
    }
    mBaseUri = uri.toString();
    mHostName = hostname;

    //TODO Fix SSL problem before exchanging user data
    // workaround for SSL problems, may lower the security level (man-in-the-middle-attack possible)
    // Android does not use the correct SSL certificate for wger.de and throws the exception 
    // javax.net.ssl.SSLException: hostname in certificate didn't match: <wger.de> != <vela.uberspace.de> OR <vela.uberspace.de> OR <uberspace.de> OR <*.vela.uberspace.de>
    // issue is not too serious as no user data is exchanged at the moment
    Log.w(TAG,
            "OpenTraining will accept all SSL-certificates. This issue has to be fixed before exchanging real user data.");
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    ClientConnectionManager mgr = new ThreadSafeClientConnManager(client.getParams(), registry);
    mClient = new DefaultHttpClient(mgr, client.getParams());

    mClient.setRedirectHandler(sRedirectHandler);
    mClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);

    // Set verifier     
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    // set user agent
    USER_AGENT = "opentraining/" + versionCode;
}

From source file:jsonbroker.library.client.http.HttpDispatcher.java

public HttpDispatcher(NetworkAddress networkAddress) {

    _networkAddress = networkAddress;//ww w  .ja va  2 s. com

    /*
      with ... 
      _client = new DefaultHttpClient();
      ... we get the following error ... 
      "Invalid use of SingleClientConnManager: connection still allocated.\nMake sure to release the connection before allocating another one."
      ... using a thread safe connecion manager ... 
      * http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
      * http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/ 
     */

    //sets up parameters
    HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf-8");
    params.setBooleanParameter("http.protocol.expect-continue", false);

    // timeouts ... 
    HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
    HttpConnectionParams.setSoTimeout(params, 5 * 1000);

    //registers schemes for both http and https
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    registry.register(new Scheme("https", sslSocketFactory, 443));

    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
    _client = new DefaultHttpClient(manager, params);

}

From source file:com.nookdevs.library.Smashwords.java

private boolean authenticate() {
    String url = AUTH_URL;
    try {//www .j  a v a2s.c  om
        nookLib.waitForNetwork(lock);
        SSLSocketFactory factory = SSLSocketFactory.getSocketFactory();
        X509HostnameVerifier orgVerifier = factory.getHostnameVerifier();
        factory.setHostnameVerifier(new AllowAllHostnameVerifier());
        HttpPost request = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", m_User));
        nvps.add(new BasicNameValuePair("password", m_Pass));
        request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(request);
        parseBookShelf(response.getEntity().getContent());
        factory.setHostnameVerifier(orgVerifier);
        lock.release();
        return true;
    } catch (Exception ex) {
        Log.e("Smashwords", "exception during authentication", ex);
        return false;
    }
}

From source file:com.wialon.remote.ApacheSdkHttpClient.java

private void initDefaultClient() {
    BasicHttpParams httpParams = getBasicHttpParams(DEFAULT_SOCKET_TIMEOUT);
    KeyStore trustStore;//from w  w  w . j av a  2 s.  c  om
    SSLSocketFactory sf = null;
    try {
        trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        sf = new TrustAllSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (Exception e) {
        e.printStackTrace();
    }
    registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    if (sf != null)
        registry.register(new Scheme("https", sf, 443));
    ThreadSafeClientConnManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, registry);
    defaultHttpClient = new DefaultHttpClient(clientConnectionManager, httpParams);
}

From source file:li.klass.fhem.fhem.FHEMWEBConnection.java

private DefaultHttpClient createNewHTTPClient(int connectionTimeout, int socketTimeout) {
    try {//from   w w  w  .j  a v a 2 s  . co m
        KeyStore trustStore;
        SSLSocketFactory socketFactory;

        trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        socketFactory = new CustomSSLSocketFactory(trustStore);
        socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        HttpConnectionParams.setSoTimeout(params, socketTimeout);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", socketFactory, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}