Example usage for org.apache.http.client HttpClient getConnectionManager

List of usage examples for org.apache.http.client HttpClient getConnectionManager

Introduction

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

Prototype

@Deprecated
ClientConnectionManager getConnectionManager();

Source Link

Document

Obtains the connection manager used by this client.

Usage

From source file:ro.teodorbaciu.commons.client.ws.util.WebClientDevWrapper.java

/**
 * Provides a new instance of http client that wraps the 
 * instance specified as parameter./*from  w ww .  java2s  .c  o  m*/
 */
@SuppressWarnings("deprecation")
public static DefaultHttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            @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 string, SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:uk.bcu.services.ItunesSearchService.java

/** This is making api calls */
public void run() {
    //String api_key = "096174e14f0efd0be330fce5f1f84de2";

    String url = //"https://itunes.apple.com/search?term="+query1+"&entity=album";
            "http://itunes.apple.com/search?term=" + query1 + "&media=movie";
    //"http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=" +
    //   api_key+ "&q="+query;

    boolean error = false;
    HttpClient httpclient = null;
    try {//from www.j  av  a 2s .c  o  m
        httpclient = new DefaultHttpClient();
        HttpResponse data = httpclient.execute(new HttpGet(url));
        HttpEntity entity = data.getEntity();
        String result = EntityUtils.toString(entity, "UTF8");

        JSONObject json = new JSONObject(result);
        if (json.getInt("resultCount") > 0) {
            resultss = json.getJSONArray("results");
        } else {
            error = true;
        }
    } catch (Exception e) {
        resultss = null;
        error = true;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    super.serviceComplete(error);
}

From source file:com.base.net.volley.toolbox.HttpClientStack.java

/**
 * https?// w  w  w .j a va2  s  . c  o m
 * @param client
 */
private void setClientHttps(HttpClient client) {

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // ??
        ClientConnectionManager conManager = client.getConnectionManager();
        SchemeRegistry schReg = conManager.getSchemeRegistry();
        if (schReg == null) {
            schReg = new SchemeRegistry();
        }
        schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schReg.register(new Scheme("https", sf, 443));

    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.wildfly.core.test.standalone.mgmt.api.core.ResponseAttachmentTestCase.java

private void shutdownHttpClients() {
    for (Iterator<HttpClient> iter = httpClients.iterator(); iter.hasNext();) {
        HttpClient client = iter.next();
        try {/*from  w w w .j a  va 2  s .c  o  m*/
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            client.getConnectionManager().shutdown();
        } catch (Exception e) {
            log.error(e);
        } finally {
            iter.remove();
        }
    }
    httpClients.clear();
}

From source file:org.jasig.portlet.newsreader.model.RemoteHttpFullStory.java

private String fetchRemoteContent() throws ClientProtocolException, IOException {
    log.trace("RemoteHttpFullStory.fetchRemoteContent()");
    HttpClient httpclient = new DefaultHttpClient();
    try {//from   w  w  w. j a  v a2  s  .c o m
        HttpGet httpget = new HttpGet(remoteHttpUrl);

        log.debug("executing request " + httpget.getURI());

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        return responseBody;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:kcb.billerengine.processors.CallJSON.java

private String getJson(HttpClient httpClient, String jsonURL, String body) {
    String sb = null;/*from   w  w  w . j  a v  a2s.  co m*/
    try {
        System.out.println("JSON REQUEST: " + body);
        StringEntity params = new StringEntity(body);

        HttpPost request = new HttpPost(jsonURL);
        request.addHeader("content-type", "application/text");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);

        sb = EntityUtils.toString(response.getEntity(), "UTF-8");

    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
    System.out.println("ECITIZEN RESPONSE : " + sb);
    httpClient.getConnectionManager().shutdown();
    return sb;
}

From source file:org.apache.whirr.util.integration.BlobCacheTest.java

private String readContent(HttpRequest req) throws IOException {
    HttpClient client = new DefaultHttpClient();
    try {//w ww .  java2  s .c o m
        HttpGet get = new HttpGet(req.getEndpoint());

        Map<String, Collection<String>> headers = req.getHeaders().asMap();
        for (String key : headers.keySet()) {
            for (String value : headers.get(key)) {
                get.addHeader(key, value);
            }
        }

        ResponseHandler<String> handler = new BasicResponseHandler();
        return client.execute(get, handler);

    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:org.neo4j.server.rest.RetrieveNodeDocIT.java

@Test
public void shouldParameteriseUrisInNodeRepresentationWithHostHeaderValue() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {// w  w  w.j  ava  2s  .c om
        HttpGet httpget = new HttpGet(nodeUri);

        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("Host", "dummy.neo4j.org");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        String entityBody = IOUtils.toString(entity.getContent(), "UTF-8");

        assertThat(entityBody, containsString("http://dummy.neo4j.org/db/data/node/"));

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.wso2.carbon.apimgt.impl.utils.CommonUtil.java

/**
 * Validate the backend by sending HTTP HEAD
 *
 * @param urlVal - backend URL/*from www .  ja  v  a2 s .  c  o  m*/
 * @return - status of HTTP HEAD Request to backend
 */
public static String sendHttpHEADRequest(String urlVal) {

    String response = "error while connecting";

    HttpClient client = new DefaultHttpClient();
    HttpHead head = new HttpHead(urlVal);
    client.getParams().setParameter("http.socket.timeout", 4000);
    client.getParams().setParameter("http.connection.timeout", 4000);

    if (System.getProperty(APIConstants.HTTP_PROXY_HOST) != null
            && System.getProperty(APIConstants.HTTP_PROXY_PORT) != null) {
        if (log.isDebugEnabled()) {
            log.debug("Proxy configured, hence routing through configured proxy");
        }
        String proxyHost = System.getProperty(APIConstants.HTTP_PROXY_HOST);
        String proxyPort = System.getProperty(APIConstants.HTTP_PROXY_PORT);
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(proxyHost, new Integer(proxyPort)));
    }

    try {
        HttpResponse httpResponse = client.execute(head);
        int statusCode = httpResponse.getStatusLine().getStatusCode();

        //If the endpoint doesn't support HTTP HEAD or if status code is < 400
        if (statusCode == 405 || statusCode % 100 < 4) {
            if (log.isDebugEnabled() && statusCode == 405) {
                log.debug("Endpoint doesn't support HTTP HEAD");
            }
            response = "success";
        }
    } catch (IOException e) {
        // sending a default error message.
        log.error("Error occurred while connecting backend : " + urlVal + ", reason : " + e.getMessage(), e);
    } finally {
        client.getConnectionManager().shutdown();
    }
    return response;
}