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:androidmapsdefinitivo.android.bajaintec.com.androidmapsdefinitivo.Control.RestClient.java

private void executeRequest(HttpUriRequest request, String url) {
    HttpClient client = new DefaultHttpClient();

    HttpResponse httpResponse;/*  www  .jav a  2 s. co m*/

    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    }
}

From source file:co.cask.cdap.passport.http.client.PassportClient.java

private String request(HttpUriRequest uri) {
    LOG.trace("Requesting " + uri.getURI().toASCIIString());
    HttpClient client = new DefaultHttpClient();
    try {/*from  www .j  ava2s  .co  m*/
        HttpResponse response = client.execute(uri);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    String.format("Call failed with status : %d", response.getStatusLine().getStatusCode()));
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ByteStreams.copy(response.getEntity().getContent(), bos);
        return bos.toString("UTF-8");
    } catch (IOException e) {
        LOG.warn("Failed to retrieve data from " + uri.getURI().toASCIIString(), e);
        return null;
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:com.universalmind.core.components.mockup.loremipsum.BaseballIpsumService.java

@Test
public void testService() {
    Integer paragraphs = 2;//  w w  w .ja va2s.  c  om

    String loremText = null;
    String url = null;
    url = "http://baseballipsum.apphb.com/api/?startwithlorem=true&paras=" + paragraphs;

    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);

        loremText = parseBaseballIpsum(responseBody);

        Assert.assertNotNull(loremText);
        Assert.assertTrue(loremText.length() > 100);
    } catch (Exception jse) {
        //do nothing
        jse.printStackTrace();
    } 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:com.couchbase.capi.TestCouchbase.java

public void testPoolBuckets() throws Exception {
    HttpClient client = getClient();

    // first access the buckets list with the correct uuid
    HttpUriRequest request = new HttpGet(String
            .format("http://localhost:%d/pools/default/buckets?uuid=00000000000000000000000000000000", port));
    HttpResponse response = client.execute(request);
    validateSuccessfulBucketsResponse(response);

    // now access it with the wrong uuid
    request = new HttpGet(String
            .format("http://localhost:%d/pools/default/buckets?uuid=00000000000000000000000000000001", port));
    response = client.execute(request);/*from w w w  .  j a v a  2  s .  c o  m*/
    validateMissingBucketsResponse(response);

    client.getConnectionManager().shutdown();
}

From source file:de.unidue.inf.is.ezdl.dlservices.library.manager.referencesystems.bibsonomy.BibsonomyUtils.java

/** send a POST request and return the response as string */
String sendPutRequest(String url, String bodytext) throws Exception {
    String sresponse;//from  ww w.j  a va 2 s  .  co m

    HttpClient httpclient = new DefaultHttpClient();
    HttpPut httpput = new HttpPut(url);

    // add authorization header
    httpput.addHeader("Authorization", authHeader);

    StringEntity body = new StringEntity(bodytext);
    httpput.setEntity(body);

    HttpResponse response = httpclient.execute(httpput);

    // check statuscode
    if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK
            || response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_CREATED) {
        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, "Bibsonomy Error", XMLUtils.parseError(sresponse));
    }
    return sresponse;

}

From source file:ch.ethz.inf.vs.android.g54.a4.net.RequestHandler.java

/**
 * Execute an HTTP get on a given resource on the configured server
 * /*from  w  w w .  j  av a  2s . co m*/
 * @param res
 *            The resource URL without host
 * @return a JSONObject / JSONArray
 * @throws ServerException
 * @throws ConnectionException
 * @throws UnrecognizedResponseException
 */
public Object request(String res) throws ServerException, ConnectionException, UnrecognizedResponseException {
    Log.d(TAG, String.format("Sending request for resource %s.", res));

    HttpClient client = new DefaultHttpClient();
    String responseBody = null;
    try {
        HttpGet htget = new HttpGet(HOST + ":" + PORT + res);
        BasicHeader header = new BasicHeader("Accept", "application/json");
        htget.addHeader(header);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseBody = client.execute(htget, responseHandler);
    } catch (ClientProtocolException e) {
        throw new ServerException("Server returned an error.", e);
    } catch (IOException e) {
        throw new ConnectionException("Could not connect to server.", e);
    } finally {
        client.getConnectionManager().shutdown();
    }
    return parseResponse(responseBody);
}

From source file:com.expensify.testframework.RestClient.java

private void executeRequest(HttpUriRequest request, String url) {
    HttpClient client = new DefaultHttpClient();

    HttpResponse httpResponse;/*from  w  ww  .ja  v  a2 s  . c  o  m*/

    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException cpExc) {
        client.getConnectionManager().shutdown();
        cpExc.printStackTrace();
    } catch (IOException ioExc) {
        client.getConnectionManager().shutdown();
        ioExc.printStackTrace();
    }
}

From source file:de.unidue.inf.is.ezdl.dlservices.library.manager.referencesystems.bibsonomy.BibsonomyUtils.java

/** send a POST request and return the response as string */
String sendPostRequest(String url, String bodytext) throws Exception {
    String sresponse;/*from  ww w .j  a  va2  s.c  o m*/

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    // add authorization header
    httppost.addHeader("Authorization", authHeader);

    StringEntity body = new StringEntity(bodytext);
    httppost.setEntity(body);

    HttpResponse response = httpclient.execute(httppost);

    // check statuscode
    if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK
            || response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_CREATED) {
        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, "Bibsonomy Error", XMLUtils.parseError(sresponse));
    }
    return sresponse;

}

From source file:com.universalmind.core.components.mockup.loremipsum.BaconIpsumService.java

@Test
public void testService() {
    Integer paragraphs = 2;//from  w  w w. java  2  s  .c om

    String loremText = null;
    String url = null;
    url = "http://baconipsum.com/api/?type=meat-and-filler&start-with-lorem=1&paras=" + paragraphs;

    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);

        loremText = parseBaconIpsum(responseBody);

        Assert.assertNotNull(loremText);
        Assert.assertTrue(loremText.length() > 100);
    } catch (Exception jse) {
        //do nothing
        jse.printStackTrace();
    } 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:ca.sqlpower.enterprise.DataSourceCollectionUpdater.java

protected void postJDBCDataSourceProperties(JDBCDataSource ds, List<NameValuePair> properties) {
    if (postingProperties)
        return;//w  ww . j a v  a2  s .co m

    HttpClient httpClient = getHttpClient();
    try {
        URI jdbcDataSourceURI = jdbcDataSourceURI(ds);
        try {
            HttpPost request = new HttpPost(jdbcDataSourceURI);
            request.setEntity(new UrlEncodedFormEntity(properties));
            httpClient.execute(request, responseHandler);
        } catch (IOException ex) {
            throw new RuntimeException("Server request failed at " + jdbcDataSourceURI, ex);
        }
    } catch (URISyntaxException ex) {
        throw new RuntimeException(ex);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}