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:org.zywx.wbpalmstar.platform.push.report.PushReportHttpClient.java

public static boolean isNetWork() {
    HttpGet get = new HttpGet("http://www.baidu.com");
    HttpResponse httpResponse = null;//  w w w  . java 2 s .  c  o m
    HttpClient httpClient = Http.getHttpsClient(60 * 1000);
    get.setHeader("Accept", "*/*");
    try {
        // httpClient = new DefaultHttpClient(setRedirecting());
        httpResponse = httpClient.execute(get);
        int responesCode = httpResponse.getStatusLine().getStatusCode();
        BDebug.d("debug", "responesCode == " + responesCode);
        if (responesCode == 200) {
            // ?
            return true;
        }
    } catch (ClientProtocolException e) {

        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    } catch (Exception e) {

        e.printStackTrace();
    } finally {
        if (get != null) {
            get.abort();
            get = null;
        }
        if (httpResponse != null) {
            httpResponse = null;
        }
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
            httpClient = null;
        }
    }
    return false;
}

From source file:wsattacker.http.transport.TlsWrapperClient.java

public static HttpClient wrapClient(HttpClient base) {
    try {//from   w  w  w  . j  ava  2 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() {
            @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;
            }

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }
        };
        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 (NoSuchAlgorithmException ex) {
        return null;
    } catch (KeyManagementException ex) {
        return null;
    }
}

From source file:de.koczewski.maxapi.WebClientDevWrapper.java

public static DefaultHttpClient wrapClient(HttpClient base) {
    try {/*from   w ww  .ja  v  a 2  s . co m*/
        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() {

            @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) {
        throw new RuntimeException(ex);
    }
}

From source file:com.wrmsr.neurosis.aws.client.WebClientDevWrapper.java

public static HttpClient wrapClient(HttpClient base) {
    try {//  w  w w.j av a 2  s. c o m
        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() {

            @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:com.optimusinfo.elasticpath.cortex.common.Utils.java

/**
 * //from w w  w  . j  a v a2s .  co  m
 * @param accessToken
 * @param postURL
 * @param ent
 * @param contentType
 * @return
 */
public static int postData(String postUrl, JSONObject requestBody, String accessToken, String contentType,
        String accessTokenInitializer) {
    HttpClient client = new DefaultHttpClient();
    try {

        HttpPost postRequest = new HttpPost(postUrl);
        postRequest.setHeader(Constants.RequestHeaders.CONTENT_TYPE_STRING, contentType);
        postRequest.setHeader(Constants.RequestHeaders.AUTHORIZATION_STRING,
                accessTokenInitializer + " " + accessToken);

        if (requestBody != null) {
            StringEntity requestEntity = new StringEntity(requestBody.toString());
            requestEntity.setContentEncoding("UTF-8");
            requestEntity.setContentType(contentType);
            postRequest.setEntity(requestEntity);
        }

        HttpResponse responsePOST = client.execute(postRequest);
        Log.i("POST RESPONSE", EntityUtils.toString(responsePOST.getEntity()));

        return responsePOST.getStatusLine().getStatusCode();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } finally {
        client.getConnectionManager().shutdown();
    }
    return 0;
}

From source file:org.bitrepository.protocol.http.HttpFileExchange.java

@Override
public void deleteFromServer(URL url) throws IOException, URISyntaxException {
    HttpClient httpClient = null;
    try {/*from w ww .j  a  v a 2 s. c om*/
        httpClient = getHttpClient();
        HttpDelete deleteOperation = new HttpDelete(url.toURI());
        httpClient.execute(deleteOperation);
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }
}

From source file:ch.netcetera.eclipse.common.net.AbstractHttpClient.java

private void configureSslHandling(HttpClient httpClient) throws CoreException {
    Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
    Scheme https = new Scheme("https", 443, SSLSocketFactory.getSocketFactory());
    SchemeRegistry sr = httpClient.getConnectionManager().getSchemeRegistry();
    sr.register(http);/*from  w  ww. j  a  v a2 s  . c  o  m*/
    sr.register(https);
}

From source file:com.foundationdb.http.HttpMonitorVerifySSLIT.java

/**
 * This code sets up the httpclient to accept any SSL certificate. The 
 * SSL certificate generated by the instructions above is not correctly
 * signed, so we need ignore the problem. 
 * This code should not, under any circumstances, be allowed anywhere 
 * the production code. // ww w  .  jav  a 2s.  c  o m
 * @param base
 * @return
 */
private HttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");

        ctx.init(null, new TrustManager[] { getTrustManager() }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 8091));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:io.werval.maven.StartMojoIT.java

@Test
public void startMojoIntegrationTest() throws IOException, InterruptedException {
    final Holder<Exception> errorHolder = new Holder<>();
    Thread runThread = new Thread(newRunnable(errorHolder, "werval:start"), "maven-werval-start-thread");
    try {//from   w w w.  j a v a2  s . c  om
        runThread.start();

        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://localhost:23023/");
        ResponseHandler<String> handler = new BasicResponseHandler();

        await().atMost(60, SECONDS).pollDelay(5, SECONDS).until(() -> {
            try {
                return client.execute(get, handler);
            } catch (Exception ex) {
                return null;
            }
        }, containsString("I ran!"));

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

From source file:com.navercorp.pinpoint.plugin.httpclient4.HttpClientIT.java

@Test
public void test() throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
    try {/*  ww w . j a v  a 2 s . c om*/
        HttpPost post = new HttpPost("http://www.naver.com");
        post.addHeader("Content-Type", "application/json;charset=UTF-8");

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        httpClient.execute(post, responseHandler);
    } catch (Exception ignored) {
    } finally {
        if (null != httpClient && null != httpClient.getConnectionManager()) {
            httpClient.getConnectionManager().shutdown();
        }
    }

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    Class<?> connectorClass;

    try {
        connectorClass = Class.forName("org.apache.http.impl.conn.ManagedClientConnectionImpl");
    } catch (ClassNotFoundException e) {
        connectorClass = Class.forName("org.apache.http.impl.conn.AbstractPooledConnAdapter");
    }

    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL",
            AbstractHttpClient.class.getMethod("execute", HttpUriRequest.class, ResponseHandler.class)));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL",
            connectorClass.getMethod("open", HttpRoute.class, HttpContext.class, HttpParams.class),
            annotation("http.internal.display", "www.naver.com")));
    verifier.verifyTrace(event("HTTP_CLIENT_4",
            HttpRequestExecutor.class.getMethod("execute", HttpRequest.class, HttpClientConnection.class,
                    HttpContext.class),
            null, null, "www.naver.com", annotation("http.url", "/"), annotation("http.status.code", 200),
            annotation("http.io", anyAnnotationValue())));
    verifier.verifyTraceCount(0);
}