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:csiro.pidsvc.helper.Http.java

public static String simpleGetRequestStrict(String uri) {
    HttpClient httpClient = new DefaultHttpClient();
    try {//from   w  w  w  .  jav  a2s  . co  m
        HttpGet httpGet = new HttpGet(uri);

        // Get the data.
        HttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != 200)
            return null;
        HttpEntity entity = response.getEntity();

        // Return content.
        return EntityUtils.toString(entity);
    } catch (Exception e) {
        _logger.warn("Exception occurred while executing an HTTP request.", e);
        return null;
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.cloudhopper.httpclient.util.HttpClientFactory.java

/**
 * Adding support for SSL mutual authentication using specified keystore/truststore.
 * Specifying keystore/truststore is optional. If unspecified, a normal SSL scheme
 * is created.//from w ww  .  j a  v a  2 s  . c o m
 */
static public void configureWithSslKeystoreTruststore(HttpClient client, File keystoreFile,
        String keystorePassword, File truststoreFile, String truststorePassword)
        throws CertificateException, FileNotFoundException, IOException, KeyStoreException,
        KeyManagementException, NoSuchAlgorithmException, UnrecoverableKeyException {

    //
    // create a new https scheme with no SSL verification
    //
    Scheme httpsScheme = SchemeFactory.createHttpsScheme(keystoreFile, keystorePassword, truststoreFile,
            truststorePassword);
    //
    // register this new scheme on the https client
    //
    client.getConnectionManager().getSchemeRegistry().register(httpsScheme);
}

From source file:ee.ioc.phon.netspeechapi.Utils.java

/**
 * <p>Executes the given HTTP request using the given HTTP client,
 * and returns the received entity as string.
 * Returns <code>null</code> if the query was performed (i.e. the server
 * was reachable) but resulted in a failure,
 * e.g. 404 error.</p>/*from w  w w.ja v a 2s .  c  o  m*/
 * 
 * @param client HTTP client
 * @param request HTTP request (e.g. GET or POST)
 * @return response as String
 * @throws IOException
 */
public static String getResponseEntityAsString(HttpClient client, HttpUriRequest request) throws IOException {
    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();

        if (entity == null) {
            return null;
        }
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }
        if (entity.getContentEncoding() == null) {
            return EntityUtils.toString(entity, HTTP.UTF_8);
        }
        return EntityUtils.toString(entity);
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:com.base.httpclient.HttpJsonClient.java

/**
 * post//  w  w  w.  j  a  va2 s. c o  m
 * 
 * @param url
 *            URL
 * @param data
 *            
 * @return 
 * @throws ClientProtocolException
 * @throws IOException
 */
public static String post(String url, String data) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpPost httpPost = new HttpPost(url);

        if (data != null) {
            ByteArrayEntity mult = new ByteArrayEntity(data.getBytes("UTF-8"));
            httpPost.setEntity(mult);
        }

        log.debug("begin to post url:" + url);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return httpclient.execute(httpPost, responseHandler);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.datacleaner.util.SecurityUtils.java

/**
 * Removes the certificate checks of HTTPS traffic on a HTTP client. Use
 * with caution!/*from  w  w  w  . ja v a  2  s  .com*/
 * 
 * @param httpClient
 * @throws IllegalStateException
 */
public static void removeSshCertificateChecks(HttpClient httpClient) throws IllegalStateException {
    try {
        // prepare a SSL context which doesn't validate certificates
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        final TrustManager trustManager = new NaiveTrustManager();
        sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
        final SSLSocketFactory schemeSocketFactory = new SSLSocketFactory(sslContext);
        final Scheme sslScheme = new Scheme("https", 443, schemeSocketFactory);

        // try again with a new registry
        final SchemeRegistry registry = httpClient.getConnectionManager().getSchemeRegistry();
        registry.register(sslScheme);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.evrythng.java.wrapper.core.api.ApiCommand.java

private static HttpClient wrapClient(final HttpClient base) {

    try {//from w  w w . j  av a2s. c o  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory ssf = new WrapperSSLSocketFactory(trustStore);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_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) {
        return null;
    }
}

From source file:edu.mit.media.funf.IOUtils.java

public static String httpGet(String uri, HttpParams params) {
    String responseBody = null;//from   w w  w. ja va 2  s .c  o m
    HttpClient httpclient = new DefaultHttpClient();
    StringBuilder uriBuilder = new StringBuilder(uri);
    HttpGet httpget = new HttpGet(uriBuilder.toString());
    if (params != null) {
        httpget.setParams(params);
    }
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        responseBody = httpclient.execute(httpget, responseHandler);
    } catch (ClientProtocolException e) {
        Log.e(TAG, "HttpGet Error: ", e);
        responseBody = null;
    } catch (IOException e) {
        Log.e(TAG, "HttpGet Error: ", e);
        responseBody = null;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return responseBody;
}

From source file:com.markwatson.linkeddata.DBpediaLookupClientJson.java

public static String lookup(String query) {
    StringBuffer sb = new StringBuffer();
    try {//  w w w .  j  av a 2 s  . com
        HttpClient httpClient = new DefaultHttpClient();
        String query2 = query.replaceAll(" ", "+");
        HttpGet getRequest = new HttpGet(
                "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
        getRequest.addHeader("accept", "application/json");
        HttpResponse response = httpClient.execute(getRequest);
        if (response.getStatusLine().getStatusCode() != 200)
            return "Server error";

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        httpClient.getConnectionManager().shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:jp.co.conit.sss.sn.ex2.util.SNApiUtil.java

/**
 * POST????<br>/*  w ww.j ava2  s.  co m*/
 * Exception????????<br>
 * HTTP?SUCCESS???????SNServerResult???????
 * 
 * @param url
 * @param postData
 * @param result
 * @return
 */
private static SNServerResult post(String url, List<NameValuePair> postData, SNServerResult result) {
    Log.d("SN", "POST url:" + url);
    for (NameValuePair nvp : postData) {
        Log.d("SN", "POST NameValuePair:" + "KEY:" + nvp.getName() + "   VALUE:" + nvp.getValue());
    }

    try {
        HttpClient httpCli = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setEntity(new UrlEncodedFormEntity(postData, "utf-8"));

        HttpResponse response = httpCli.execute(post);
        int status = response.getStatusLine().getStatusCode();
        result.mHttpStatus = status;

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String responseBodyText = EntityUtils.toString(entity);
            entity.consumeContent();
            httpCli.getConnectionManager().shutdown();
            result.mResponseString = responseBodyText;
            Log.d("SN", "POST response:" + responseBodyText);
        }
    } catch (Exception e) {
        result.mCauseException = e;
        e.printStackTrace();
    }
    return result;
}

From source file:org.syphr.mythtv.ws.impl.ServiceUtils.java

public static String getVersion(URI serviceBaseUri) throws IOException {
    URI uri = URI.create(serviceBaseUri.toString() + "/" + VERSION_URI_PATH);

    HttpClient httpclient = new DefaultHttpClient();
    try {/* www.j a  v a 2  s  .c om*/
        HttpGet httpget = new HttpGet(uri);
        LOGGER.debug("Retrieving service version from {}", httpget.getURI());

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        LOGGER.trace("Version response: {}", responseBody);

        Matcher matcher = VERSION_PATTERN.matcher(responseBody);
        if (matcher.matches()) {
            return matcher.group(1);
        }

        throw new IOException("Failed to retrieve version information");
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}