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:de.unidue.inf.is.ezdl.dlservices.library.manager.referencesystems.connotea.ConnoteaUtils.java

/** send a GET Request and return the response as string */
String sendGetRequest(String url) throws Exception {
    String sresponse;/*from  w  ww  .  j  a v a 2s .c om*/
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);

    // set authentication header
    httpget.addHeader("Authorization", authHeader);

    HttpResponse response = httpclient.execute(httpget);

    // Check status code
    if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
        HttpEntity entity = response.getEntity();
        sresponse = readResponseStream(entity.getContent());
        httpclient.getConnectionManager().shutdown();
    } else {
        HttpEntity entity = response.getEntity();
        sresponse = readResponseStream(entity.getContent());

        String httpcode = Integer.toString(response.getStatusLine().getStatusCode());
        httpclient.getConnectionManager().shutdown();
        throw new ReferenceSystemException(httpcode, "Connotea Error", RDFUtils.parseError(sresponse));
    }
    return sresponse;
}

From source file:com.lostad.app.base.util.RequestUtil.java

public static String postJson(String url, String token, String dataJson) throws Exception {
    String json = null;/*from  w  w w. j  a  va  2s. c o m*/
    HttpEntity resEntity = null;
    HttpClient client = HttpClientManager.getHttpClient(true);
    HttpPost post = new HttpPost(url);
    if (Validator.isNotEmpty(token)) {
        post.addHeader("token", token);
    }
    try {
        post.addHeader("Content-Type", "application/json;charset=UTF-8");
        StringEntity se = new StringEntity(dataJson, "UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);
        //
        HttpResponse response = client.execute(post, new BasicHttpContext());
        resEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 200
            // ?
            json = EntityUtils.toString(resEntity);
        }
    } catch (Exception e) {
        if (post != null) {
            post.abort();//
        }
        throw e;
    } finally {

        if (resEntity != null) {
            try {
                resEntity.consumeContent();//?

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        client.getConnectionManager().closeExpiredConnections();
        ///client.getConnectionManager().shutdown();
    }
    return json;
}

From source file:com.lostad.app.base.util.RequestUtil.java

public static String postRequest(final String url, final StringEntity params, String token,
        final boolean isSingleton) throws Exception {
    String json = null;//w  w w  .j  a  va2 s. c  om
    HttpClient client = HttpClientManager.getHttpClient(isSingleton);
    HttpEntity resEntity = null;
    HttpPost post = new HttpPost(url);
    if (Validator.isNotEmpty(token)) {
        post.addHeader("token", token);
    }

    try {
        //long t1 = System.currentTimeMillis();

        if (params != null) {
            params.setContentEncoding(HTTP.UTF_8);
            params.setContentType("application/x-www-form-urlencoded");
            post.setEntity(params);
        }

        HttpResponse response = client.execute(post, new BasicHttpContext());
        resEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 200
            //?
            json = EntityUtils.toString(resEntity);
        }
    } catch (Exception e) {
        if (post != null) {
            post.abort();
        }
        throw e;
    } finally {

        if (resEntity != null) {
            try {
                resEntity.consumeContent();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        client.getConnectionManager().closeExpiredConnections();
    }
    return json;
}

From source file:ca.sqlpower.enterprise.DataSourceCollectionUpdater.java

/**
 * Handles deleting of a database entry by requesting that the server
 * deletes it. Also unlistens to the data source to prevent memory
 * leaks.//from w  w  w.  ja  v a 2  s  .c  o m
 */
@Override
public void databaseRemoved(DatabaseListChangeEvent e) {
    HttpClient httpClient = getHttpClient();
    try {
        SPDataSource removedDS = e.getDataSource();
        HttpDelete request = new HttpDelete(jdbcDataSourceURI(removedDS));
        httpClient.execute(request, responseHandler);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:nl.esciencecenter.osmium.JobLauncherService.java

/**
 * Enable insecure SSL in http client like self signed certificates.
 *
 * @param httpClient http client with secure SSL enabled
 * @throws NoSuchAlgorithmException when a particular cryptographic algorithm is requested but is not available in the environment.
 * @throws KeyManagementException if key management fails
 * @throws KeyStoreException if key store fails
 * @throws UnrecoverableKeyException if key is unrecoverable
 *//*from  ww w . jav  a  2s.  co m*/
public void useInsecureSSL(HttpClient httpClient)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    SSLSocketFactory socketFactory;
    socketFactory = new SSLSocketFactory(new TrustStrategy() {

        public boolean isTrusted(final X509Certificate[] chain, String authType) throws CertificateException {
            // Oh, I am easy...
            return true;
        }

    }, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    SchemeRegistry registry = httpClient.getConnectionManager().getSchemeRegistry();
    registry.register(new Scheme("https", HTTPS_PORT, socketFactory));
}

From source file:com.google.appengine.tck.blobstore.BlobstoreServeTest.java

@Test
@RunAsClient/*  www.j  ava 2 s .com*/
public void testUploadedFileHasCorrectContent_upload(@ArquillianResource URL url) throws Exception {
    FileUploader fileUploader = new FileUploader();
    String uploadUrl = fileUploader.getUploadUrl(new URL(url, "getUploadUrl"));
    final String blobKey = fileUploader.uploadFile(uploadUrl, "file", FILENAME, CONTENT_TYPE, UPLOADED_CONTENT);

    final String content = new String(UPLOADED_CONTENT);
    final URI uri = new URL(url, "blobserviceserve?blobKey=" + blobKey).toURI();
    final HttpClient client = new DefaultHttpClient();
    try {
        doTest(client, uri, null, null, content);
        doTest(client, uri, Collections.<Header>singleton(new BasicHeader("Range", "bytes=1-3")), null,
                content.substring(1, 3 + 1));
        doTest(client, uri, null, Collections.singletonMap("blobRange", "2"), content.substring(2));
        doTest(client, uri, null, Collections.singletonMap("blobRangeString", "bytes=2-5"),
                content.substring(2, 5 + 1));
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:net.noday.cat.web.admin.DwzManager.java

public void gendwz(String url, String alias) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://dwz.cn/create.php");
    post.getParams().setParameter("url", url).setParameter("alias", alias);
    try {/*from  w  w  w . j  a va2  s. co  m*/
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = client.execute(post, responseHandler);
        System.out.println(responseBody);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        client.getConnectionManager().shutdown();
    }
}

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

@Test
public void shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {//  w w  w  . ja v a2  s .  co  m
        HttpGet httpget = new HttpGet(nodeUri);

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

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

        assertThat(entityBody, containsString(nodeUri.toString()));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:ca.sqlpower.enterprise.DataSourceCollectionUpdater.java

protected void postJDBCDataSourceTypeProperties(JDBCDataSourceType jdst, List<NameValuePair> properties) {
    if (postingProperties)
        return;/*from   w  w w  . jav a  2s.  c o m*/

    HttpClient httpClient = getHttpClient();
    try {
        HttpPost request = new HttpPost(jdbcDataSourceTypeURI(jdst));
        request.setEntity(new UrlEncodedFormEntity(properties));
        httpClient.execute(request, responseHandler);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.java

private void captureMetrics(ExtendedHttpMethod httpMethod, HttpClient httpClient) {
    try {/*from  ww w .  ja  v a 2s .c o m*/
        httpMethod.getMetrics()
                .setIpAddress(InetAddress.getByName(httpMethod.getURI().getHost()).getHostAddress());
        httpMethod.getMetrics().setPort(httpMethod.getURI().getPort(), httpClient.getConnectionManager()
                .getSchemeRegistry().getScheme(httpMethod.getURI().getScheme()).getDefaultPort());
    } catch (UnknownHostException uhe) {
        /* ignore */
    } catch (IllegalStateException ise) {
        /* ignore */
    }
}