Example usage for org.apache.http.impl.client DefaultHttpClient getConnectionManager

List of usage examples for org.apache.http.impl.client DefaultHttpClient getConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient getConnectionManager.

Prototype

public synchronized final ClientConnectionManager getConnectionManager() 

Source Link

Usage

From source file:com.davidivins.checkin4me.oauth.OAuth2Request.java

/**
 * getTolerantClient/*from   ww w.  j av  a  2 s  .  com*/
 * 
 * Stolen from stackoverflow.com
 * http://stackoverflow.com/questions/3135679/android-httpclient-hostname-in-certificate-didnt-match-example-com-exa
 * 
 * @return DefaultttpClient
 */
public DefaultHttpClient getTolerantClient() {
    DefaultHttpClient client = new DefaultHttpClient();

    SSLSocketFactory sslSocketFactory = (SSLSocketFactory) client.getConnectionManager().getSchemeRegistry()
            .getScheme("https").getSocketFactory();

    final X509HostnameVerifier delegate = sslSocketFactory.getHostnameVerifier();

    if (!(delegate instanceof TolerantVerifier))
        sslSocketFactory.setHostnameVerifier(new TolerantVerifier(delegate));

    return client;
}

From source file:org.sonatype.nexus.error.reporting.NexusPRConnectorTest.java

@Test(expected = IllegalStateException.class)
public void testDispose() throws ConnectionPoolTimeoutException, InterruptedException {
    final DefaultHttpClient client = (DefaultHttpClient) underTest.client();
    underTest.dispose();/*from  ww w. java2s  .  co  m*/
    client.getConnectionManager().requestConnection(new HttpRoute(new HttpHost("host")), null)
            .getConnection(1000, TimeUnit.SECONDS);
}

From source file:com.betfair.testing.utils.cougar.manager.HttpPageManager.java

public int getPage(HttpPageBean bean) {

    // Get bean properties
    String requestedProtocol = bean.getProtocol();
    String requestedHost = bean.getHost();
    int requestedPort = bean.getPort();
    String requestedLink = bean.getLink();
    String username = bean.getAuthusername();
    String password = bean.getAuthpassword();

    final SSLSocketFactory sf = new SSLSocketFactory(createEasySSLContext(),
            SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme https = new Scheme("https", 9999, sf);

    // Set up httpClient to use given auth details and protocol
    DefaultHttpClient client = new DefaultHttpClient();
    client.getConnectionManager().getSchemeRegistry().register(https);
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(username, password));

    int status = -1;

    InputStream inputStream = null;
    // Make the request
    try {//from  w  ww .  jav  a  2 s.c  om
        final HttpGet httpget = new HttpGet(
                URIUtils.createURI(requestedProtocol, requestedHost, requestedPort, requestedLink, null, null));
        final HttpResponse httpResponse = client.execute(httpget);
        inputStream = httpResponse.getEntity().getContent();
        status = httpResponse.getStatusLine().getStatusCode();

        if (status == HttpStatus.SC_OK) {
            bean.setPageLoaded(true);

            byte[] buffer = new byte[(int) httpResponse.getEntity().getContentLength()];
            int read;
            int count = 0;
            while ((read = inputStream.read()) != -1) {
                buffer[count] = (byte) read;
                count++;
            }
            bean.setPageText(new String(buffer, "UTF-8"));
            bean.setBuffer(buffer);
        }
    } catch (IOException e1) {
        return -1;
    } catch (URISyntaxException e) {
        return -1;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    return status;
}

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static HttpClient getHttpsClientWithProxy(byte[] sslCertificateBytes, String proxyAddress,
        int proxyPort) {
    DefaultHttpClient httpClient;
    Certificate[] sslCertificate;
    HttpHost proxy;//from  w w  w . j  a v  a2 s  .  co m

    httpClient = new DefaultHttpClient();
    try {
        sslCertificate = convertByteArrayToCertificate(sslCertificateBytes);

        TrustManagerFactory tf = TrustManagerFactory.getInstance("X509");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null);
        for (int i = 0; i < sslCertificate.length; i++) {
            ks.setCertificateEntry("StartCom" + i, sslCertificate[i]);
        }

        tf.init(ks);
        TrustManager[] tm = tf.getTrustManagers();

        SSLContext sslCon = SSLContext.getInstance("SSL");
        sslCon.init(null, tm, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
        Scheme sch = new Scheme("https", 443, socketFactory);

        proxy = new HttpHost(proxyAddress, proxyPort, "https");
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException
            | KeyManagementException | UnrecoverableKeyException ex) {
        Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    return httpClient;
}

From source file:org.gradle.api.internal.artifacts.repositories.transport.http.HttpClientConfigurer.java

private void configureProxy(DefaultHttpClient httpClient, HttpProxySettings proxySettings) {
    // Use standard JVM proxy settings
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    httpClient.setRoutePlanner(routePlanner);

    HttpProxySettings.HttpProxy proxy = proxySettings.getProxy();
    if (proxy != null && proxy.credentials != null) {
        useCredentials(httpClient, proxy.credentials, proxy.host, proxy.port);
    }/* w  w w . java 2 s . com*/
}

From source file:com.kku.apps.pricesearch.util.HttpConnection.java

public static Bitmap getBitmapFromUrl(String url) {

    HttpGet method = new HttpGet(url);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {//from w ww  .j  a v a  2s . c om
        BufferedHttpEntity entity = httpClient.execute(method, new ResponseHandler<BufferedHttpEntity>() {

            @Override
            public BufferedHttpEntity handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {

                // Xe?[^XR?[h
                int status = response.getStatusLine().getStatusCode();

                if (HttpStatus.SC_OK != status) {
                    throw new RuntimeException("?MG?[?");
                }
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufferEntity = new BufferedHttpEntity(entity);
                return bufferEntity;
            }
        });
        InputStream is = entity.getContent();
        return BitmapFactory.decodeStream(is);
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.trancecode.xproc.step.HttpRequestStepProcessor.java

private HttpClient prepareHttpClient(final XProcHttpRequest xProcRequest, final BasicHttpContext localContext) {
    final SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(schemeRegistry);
    final DefaultHttpClient httpClient = new DefaultHttpClient(connManager);
    final ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    httpClient.setRoutePlanner(routePlanner);

    if (xProcRequest.getCredentials() != null) {
        final List<String> authPref = Lists.newArrayList(AuthPolicy.BASIC, AuthPolicy.DIGEST);
        httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPref);
        httpClient.setCredentialsProvider(xProcRequest.getCredentials());
        final AuthCache authCache = new BasicAuthCache();
        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(xProcRequest.getHttpHost(), basicAuth);
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }//from ww  w  . jav a  2s.  com
    return httpClient;
}

From source file:com.netflix.suro.server.TestStatusServer.java

private HttpResponse runQuery(String path) throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(String.format("http://localhost:%d/%s", suroServer.getStatusPort(), path));

    try {// ww  w  .j a v  a2  s . co m
        return client.execute(httpget);
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:com.ovea.facebook.client.DefaultFacebookClient.java

private HttpClient httpClient() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
    DefaultHttpClient client = new DefaultHttpClient(params);
    ClientConnectionManager ccm = client.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", 443, sslSocketFactory));
    return client;
}

From source file:cn.loveapple.client.android.util.ApiUtil.java

public static String getHttpBody(String url, PackageManager packageManager) {
    if (StringUtils.isEmpty(url)) {
        return null;
    }//from w w  w.j ava 2s . c om

    String body = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    // SYSTEM INFO
    params.setParameter("", "");
    params.setParameter("", "");
    params.setParameter("", "");
    HttpConnectionParams.setConnectionTimeout(params, 1000);
    HttpConnectionParams.setSoTimeout(params, 1000);
    HttpPost httpRequest = new HttpPost(url);
    HttpResponse httpResponse = null;

    try {
        httpResponse = httpClient.execute(httpRequest);
    } catch (Exception e) {
        Log.e(LOG_TAG, "http response execute failed.", e);
        return null;
    }
    if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity httpEntity = httpResponse.getEntity();

        try {
            body = EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            Log.e(LOG_TAG, "get http response body failed.", e);
            return null;
        } finally {
            try {
                httpEntity.consumeContent();
            } catch (IOException e) {
            }
        }
    }
    httpClient.getConnectionManager().shutdown();

    return body;
}