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:com.google.apphosting.vmruntime.VmApiProxyDelegateTest.java

private HttpClient createMockHttpClient() {
    HttpClient httpClient = mock(HttpClient.class);
    when(httpClient.getConnectionManager()).thenReturn(new PoolingClientConnectionManager());
    return httpClient;
}

From source file:com.rumblefish.friendlymusic.api.WebRequest.java

public static String webRequest(URLRequest request) {
    HttpPost httpPost = null;/*from w ww .  jav a  2  s  .  c om*/
    HttpGet httpGet = null;
    if (request.m_nameValuePairs != null) {
        httpPost = new HttpPost(request.m_serverURL);
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(request.m_nameValuePairs));
        } catch (Exception e) {
            return null;
        }
    } else {
        httpGet = new HttpGet(request.m_serverURL);
        Log.v(LOGTAG, request.m_serverURL);
    }

    HttpClient client = getNewHttpClient(request.m_timelimit);

    StringBuilder builder = new StringBuilder();

    try {
        HttpResponse response;
        if (request.m_nameValuePairs != null)
            response = client.execute(httpPost);
        else
            response = client.execute(httpGet);

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            try {
                content.close();
            } catch (IOException e) {

            }
        } else {
            Log.e("WebRequest", "Failed to download file");
            return null;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        client.getConnectionManager().closeExpiredConnections();
        client.getConnectionManager().closeIdleConnections(0, TimeUnit.NANOSECONDS);
    }

    String resultString = builder.toString();
    try {
        if (resultString == null || resultString.length() == 0) {
            return null;
        }
        return resultString;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.foundationdb.server.service.security.SecurityServiceIT.java

private int openRestURL(String request, String query, String userInfo) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(getRestURL(request, query, userInfo));
    HttpResponse response = client.execute(get);
    int code = response.getStatusLine().getStatusCode();
    EntityUtils.consume(response.getEntity());
    client.getConnectionManager().shutdown();
    return code;/* w ww.  ja v  a 2 s. c om*/
}

From source file:com.payu.sdk.helper.HttpClientHelper.java

/**
 * Sends the post request to the server//ww  w  . j  av  a  2 s  . c  om
 *
 * @param request
 *            The request to be sent to the server
 * @param requestMethod
 *            The request method to be sent to the server
 * @param the socket time out.
 * @return The response in a xml format
 * @throws ConnectionException
 * @throws SDKException
 */
public static String sendRequest(Request request, RequestMethod requestMethod, Integer socketTimeOut)
        throws PayUException, ConnectionException {

    HttpClient httpClient = new DefaultHttpClient();
    setHttpClientParameters(httpClient.getParams(), socketTimeOut);

    httpClient = WebClientDevWrapper.wrapClient(httpClient);

    try {

        HttpRequestBase httpRequest = createHttpRequest(request, requestMethod);

        HttpResponse httpResponse = httpClient.execute(httpRequest);

        if (httpResponse == null) {
            throw new ConnectionException("No response from server");
        }

        Integer httpStatus = httpResponse.getStatusLine().getStatusCode();

        Integer[] successStatus = { HttpStatus.SC_OK, HttpStatus.SC_CREATED, HttpStatus.SC_ACCEPTED };

        if (Arrays.asList(successStatus).contains(httpStatus)) {

            return getXmlResponse(httpResponse);
        } else {
            return manageResponse(httpResponse);
        }

    } catch (PayUException e) {
        throw e;
    } catch (Exception e) {
        throw new ConnectionException(e.getMessage(), e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.getcomposer.core.packagist.Downloader.java

private void registerSSLContext(HttpClient client) {

    try {//  w  w w.j a  v  a 2  s.  c  o m
        X509TrustManager tm = new ComposerTrustManager();
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = client.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:ui.shared.FreebaseHelper.java

@SuppressWarnings("deprecation")
public static HttpClient wrapClient(HttpClient base) {
    try {//ww  w .jav  a2 s.c  om
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

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

            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            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:iristk.speech.nuancecloud.NuanceCloudRecognizerListener.java

@SuppressWarnings("deprecation")
private static HttpClient getHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
    // Standard HTTP parameters
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, false);
    // Initialize the HTTP client
    HttpClient httpclient = new DefaultHttpClient(params);

    // Initialize/setup SSL
    TrustManager easyTrustManager = new X509TrustManager() {
        @Override//  ww  w.  ja  v  a  2 s  .  c om
        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
        }

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

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

    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
    SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme sch = new Scheme("https", sf, 443);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

    // Return the initialized instance of our httpclient
    return httpclient;
}

From source file:it.uiExtension.UiExtensionsTest.java

/**
 * SONAR-3555//from  w ww .j  a  va2 s.  c o m
 */
@Test
public void content_type_of_static_files_is_set() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet get = new HttpGet(orchestrator.getServer().getUrl() + "/static/uiextensionsplugin/cute.jpg");
        HttpResponse response = httpclient.execute(get);
        assertThat(response.getLastHeader("Content-Type").getValue()).isEqualTo("image/jpeg");

        EntityUtils.consume(response.getEntity());

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

From source file:net.sourceforge.subsonic.ajax.LyricsService.java

private String executeGetRequest(String url) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
    HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
    HttpGet method = new HttpGet(url);
    try {/*from   www . j a  v a2  s  . c  o m*/

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return client.execute(method, responseHandler);

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

From source file:packjacket.StaticUtils.java

/**
 * Uploads a file to the PackJacket webserver
 * @param f the fiel to upload//from   w  w  w.ja  v  a 2s. co m
 * @return the id of the log file, or 0 if it crashed
 */
public static long uploadLog(File f) {
    long id = 0;
    try {
        //Sets up the HTTP interaction
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost httppost = new HttpPost("http://packjacket.sourceforge.net/log/up.php");

        //Adds file to a POST
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(f, "image/jpeg");
        mpEntity.addPart("uploadedfile", cbFile);

        //Uplaods file
        httppost.setEntity(mpEntity);
        RunnerClass.logger.info("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        //Receives the status message
        RunnerClass.logger.info("Status: " + response.getStatusLine());
        if (resEntity != null) {
            //Get the id that the server reports
            String s = EntityUtils.toString(resEntity);
            RunnerClass.logger.info("Result: " + s);
            id = Long.parseLong(s.replaceFirst("222 ", "").replace("\n", ""));
        }
        if (resEntity != null)
            resEntity.consumeContent();

        //Close connection
        httpclient.getConnectionManager().shutdown();
    } catch (Exception ex) {
        RunnerClass.logger.info("Internet not available");
    }
    return id;
}