Example usage for org.apache.http.client HttpClient getParams

List of usage examples for org.apache.http.client HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getParams.

Prototype

@Deprecated
HttpParams getParams();

Source Link

Document

Obtains the parameters for this client.

Usage

From source file:org.mahasen.ssl.WebClientSSLWrapper.java

/**
 * @param base/*  ww w  . ja  v a2 s. c  om*/
 * @return
 */
public static HttpClient wrapClient(HttpClient base) {

    try {

        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;

            }

        };

        ctx.init(null, new TrustManager[] { tm }, 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, 443));

        return new DefaultHttpClient(ccm, base.getParams());

    } catch (Exception ex) {

        System.out.println("Error while configuring security certificate for client");
        return null;

    }

}

From source file:org.apache.camel.component.restlet.RestletRedirectTest.java

@Test
public void testRedirect() throws Exception {
    HttpGet get = new HttpGet("http://localhost:" + portNum + "/users/homer");

    // do not follow redirects
    HttpClient client = new DefaultHttpClient();
    client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);

    HttpResponse response = client.execute(get);

    for (Header header : response.getAllHeaders()) {
        log.info("Header {}", header);
    }/*w w  w . jav  a 2  s  .c om*/

    assertEquals(302, response.getStatusLine().getStatusCode());
    assertTrue("Should have location header", response.containsHeader("Location"));
    assertEquals("http://somewhere.com", response.getFirstHeader("Location").getValue());
    assertEquals("bar", response.getFirstHeader("Foo").getValue());
}

From source file:com.vintank.slack4j.webhook.SlackWebHook.java

public void send(Payload payload) throws IOException {
    HttpPost post = new HttpPost(url);

    try {// w w  w .  ja v a  2s  .co m
        post.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Slack4j/1.0");
        post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 5000);
        client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 5000);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>(1);
        nvps.add(new BasicNameValuePair("payload", MAPPER.writeValueAsString(payload)));

        post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

        client.execute(post);
    } finally {
        post.releaseConnection();
    }
}

From source file:com.google.api.client.http.apache.ApacheHttpTransportTest.java

private void checkHttpClient(HttpClient client) {
    HttpParams params = client.getParams();
    assertFalse(params.getBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true));
    assertEquals(HttpVersion.HTTP_1_1, HttpProtocolParams.getVersion(params));
}

From source file:com.guess.license.plate.Network.ThreadSafeHttpClientFactory.java

private void addUserAgent(HttpClient client) {
    String userAgent = System.getProperty("http.agent");
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
}

From source file:com.streamreduce.rest.AuthenticationResourceITCase.java

@Ignore
@Test//from  w w w . ja  v  a  2 s .  c om
public void testConsoleLoginFailure() throws Exception {

    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(HttpProtocolParams.USER_AGENT, Constants.NODEABLE_HTTP_USER_AGENT);

    //        HttpState state = client.getState();
    //        state.setCredentials(new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME),
    //                new UsernamePasswordCredentials(testUsername, "wrong_password"));
    HttpPost post = new HttpPost(getUrl());

    try {
        int status = client.execute(post).getStatusLine().getStatusCode();
        assertEquals(HttpStatus.SC_UNAUTHORIZED, status);
    } finally {
        post.releaseConnection();
    }
}

From source file:com.safecell.networking.GetLicenseKey.java

public String getRequest() {
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout

    String url = URLs.REMOTE_URL + "api/1/license_classes";

    HttpGet postRequest = new HttpGet(url);
    postRequest.setHeader("Content-Type", "application/json");

    String result = null;/*from  w ww  . j  a va 2s .co m*/
    try {
        response = client.execute(postRequest);
        result = getResponseBody();

        if (response.getStatusLine().getStatusCode() != 200) {
            response = null;
            result = null;
            failureMessage = "The licenses downlaod failed because of an unexpected error.";
        }

    } catch (Exception e) {
        response = null;
        result = null;
        failureMessage = "The licenses downlaod failed because of an unexpected error.";
    }

    return result;

}

From source file:de.hybris.platform.mpintgproductcockpit.productcockpit.util.HttpInvoker.java

public String post(final String reqURL, final String reqStr, final String charset, final int timeout) {
    // Prepare HTTP post
    final HttpPost post = new HttpPost(reqURL);
    post.setHeader("Content-Type", "application/json");

    // set content type as json and charset
    final StringEntity reqEntity = new StringEntity(reqStr, ContentType.create("application/json", charset));
    post.setEntity(reqEntity);/*  w ww .j  a  v  a  2  s  .c  om*/

    // Create HTTP client
    final HttpClient httpClient = new DefaultHttpClient();

    // set connection over time
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(timeout / 2));
    // set data loading over time
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeout / 2));

    // Execute request
    try {
        final HttpResponse response = httpClient.execute(post);
        // get response and return as String
        final HttpEntity responseEntity = response.getEntity();
        return EntityUtils.toString(responseEntity);
    } catch (final Exception e) {
        return null;
    } finally {
        post.releaseConnection();
    }
}

From source file:key.access.manager.HttpHandler.java

public boolean sendImage(String url, String employeeId, File imageFile) throws IOException {
    String userHome = System.getProperty("user.home");
    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httpPost = new HttpPost(url);
    FileBody fileBody = new FileBody(imageFile);
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("fileToUpload", fileBody);
    reqEntity.addPart("employee_id", new StringBody(employeeId));

    httpPost.setEntity(reqEntity);//from   www.j  a v a 2  s.c om

    // execute HTTP post request
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();

    if (resEntity != null) {
        String responseStr = EntityUtils.toString(resEntity).trim();
        System.out.println(responseStr);
        return true;
    } else {
        return false;
    }
}

From source file:com.thistech.spotlink.util.HttpClientFactory.java

public HttpClient newInstance() {
    HttpClient client = new DefaultHttpClient();
    HttpParams httpParams = client.getParams();
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

    if (this.properties.containsKey("httpclient.timeout")) {
        int timeout = Integer.parseInt(this.properties.getProperty("httpclient.timeout"));
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        HttpConnectionParams.setSoTimeout(httpParams, timeout);
    }/*from   w  w  w  .j a va2s. co m*/

    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRoute() {
        public int getMaxForRoute(HttpRoute route) {
            return Integer.parseInt(properties.getProperty("httpclient.conn-per-route", "5"));
        }
    });

    int totalConnections = Integer.parseInt(this.properties.getProperty("httpclient.total-connections", "100"));
    ConnManagerParams.setMaxTotalConnections(httpParams, totalConnections);

    String userAgent = this.properties.getProperty("httpclient.user-agent", "Mozilla/5.0");
    HttpProtocolParams.setUserAgent(httpParams, userAgent);

    String charset = this.properties.getProperty("httpclient.content-charset", "UTF-8");
    HttpProtocolParams.setContentCharset(httpParams, charset);

    ClientConnectionManager mgr = client.getConnectionManager();
    SchemeRegistry schemeRegistry = mgr.getSchemeRegistry();
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
    return client;
}