Java Code Examples for org.apache.http.impl.client.DefaultHttpClient#getParams()

The following examples show how to use org.apache.http.impl.client.DefaultHttpClient#getParams() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RESTClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example 2
Source File: HttpLoadTestClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    SchemeRegistry supportedSchemes = new SchemeRegistry();
    SocketFactory sf = PlainSocketFactory.getSocketFactory();
    supportedSchemes.register(new Scheme("http", sf, 80));
    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(supportedSchemes);
    connManager.setDefaultMaxPerRoute(1000);
    DefaultHttpClient client = new DefaultHttpClient(connManager);
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
    //test API call
    long t1 = System.currentTimeMillis();
    testEndpoint(client,apiEndpoint);
    long t2 = System.currentTimeMillis();
    timeElapsedForAPICall = t2 - t1;


}
 
Example 3
Source File: SOAPClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example 4
Source File: ImmersionHttpClient.java    From letv with Apache License 2.0 6 votes vote down vote up
public HttpParams getParams() {
    try {
        DefaultHttpClient defaultHttpClient = this.bнн043Dн043Dн;
        if (((b04170417041704170417З + b0417ЗЗЗЗ0417) * b04170417041704170417З) % bЗ0417ЗЗЗ0417 != bЗЗЗЗЗ0417) {
            b04170417041704170417З = b04170417ЗЗЗ0417();
            bЗЗЗЗЗ0417 = 88;
        }
        try {
            return defaultHttpClient.getParams();
        } catch (Exception e) {
            throw e;
        }
    } catch (Exception e2) {
        throw e2;
    }
}
 
Example 5
Source File: RemoteSystemClient.java    From ghwatch with Apache License 2.0 5 votes vote down vote up
protected static DefaultHttpClient prepareHttpClient(URI uri) {
  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpParams params = httpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 30000);
  HttpConnectionParams.setSoTimeout(params, 30000);
  return httpClient;
}
 
Example 6
Source File: AbstractWebIntegrationTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void createClient(String ctxPath) throws Exception {
  testProperties = new TestProperties();

  APP_BASE_PATH = testProperties.getApplicationPath("/" + ctxPath);
  LOGGER.info("Connecting to application "+APP_BASE_PATH);

  ClientConfig clientConfig = new DefaultApacheHttpClient4Config();
  clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
  client = ApacheHttpClient4.create(clientConfig);

  defaultHttpClient = (DefaultHttpClient) client.getClientHandler().getHttpClient();
  HttpParams params = defaultHttpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 3 * 60 * 1000);
  HttpConnectionParams.setSoTimeout(params, 10 * 60 * 1000);
}
 
Example 7
Source File: AbstractWebIT.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void createClient(String ctxPath) throws Exception {
  testProperties = new TestProperties();

  APP_BASE_PATH = testProperties.getApplicationPath("/" + ctxPath);
  LOGGER.info("Connecting to application "+APP_BASE_PATH);

  ClientConfig clientConfig = new DefaultApacheHttpClient4Config();
  clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
  client = ApacheHttpClient4.create(clientConfig);

  defaultHttpClient = (DefaultHttpClient) client.getClientHandler().getHttpClient();
  HttpParams params = defaultHttpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 3 * 60 * 1000);
  HttpConnectionParams.setSoTimeout(params, 10 * 60 * 1000);
}
 
Example 8
Source File: Executor.java    From blog with MIT License 5 votes vote down vote up
private static DefaultHttpClient initHttpClient()
{
	DefaultHttpClient httpClient = new DefaultHttpClient();
	HttpParams params = httpClient.getParams();
	HttpConnectionParams.setConnectionTimeout( params, connectionTimeoutMillis );
	HttpConnectionParams.setSoTimeout( params, socketTimeoutMillis );
	return httpClient;
}
 
Example 9
Source File: EmailUtil.java    From product-es with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the httpClient.
 */
public static void initialize() throws XPathExpressionException {

    DefaultHttpClient client = new DefaultHttpClient();

    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    Scheme sch = new Scheme("https", 443, socketFactory);
    ClientConnectionManager mgr = client.getConnectionManager();
    mgr.getSchemeRegistry().register(sch);
    httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}
 
Example 10
Source File: EmailUtil.java    From product-es with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the httpClient.
 */
public static void initialize() throws XPathExpressionException {

    DefaultHttpClient client = new DefaultHttpClient();

    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    Scheme sch = new Scheme("https", 443, socketFactory);
    ClientConnectionManager mgr = client.getConnectionManager();
    mgr.getSchemeRegistry().register(sch);
    httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}
 
Example 11
Source File: HttpClientTimeoutLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenUsingOldApi_whenSettingTimeoutViaParameter_thenCorrect() throws IOException {
    
    DefaultHttpClient httpClient = new DefaultHttpClient();
    int timeout = 5; // seconds
    HttpParams httpParams = httpClient.getParams();
    httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
    httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
    httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
    
    final HttpGet request = new HttpGet("http://www.github.com");
    HttpResponse execute = httpClient.execute(request);
    assertThat(execute.getStatusLine().getStatusCode(), equalTo(200));
}
 
Example 12
Source File: FetchCommentTask.java    From iZhihu with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected String doInBackground(Long... answerIds) {

    for (Long answerId : answerIds) {
        HttpGet httpGet = new HttpGet(getRequestUrl(answerId));
        httpGet.addHeader("User-Agent", USER_AGENT);
        httpGet.addHeader("Authorization", AUTHORIZATION);
        httpGet.addHeader("ZA", ZA);
        httpGet.addHeader("X-APP-VERSION", X_APP_VERSION);
        httpGet.addHeader("Cache-Control", "no-cache");

        try {
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            HttpParams httpParams = defaultHttpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_SECONDS * 1000);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_SECONDS * 1000);

            HttpResponse httpResponse = defaultHttpClient.execute(httpGet);

            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HTTP_STATUS_OK) {
                return EntityUtils.toString(httpResponse.getEntity());
            } else {
                throw new IOException(httpResponse.getStatusLine().getReasonPhrase());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}
 
Example 13
Source File: DownloadPstTask.java    From Presentation with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPreExecute() {
    mHttpClient = new DefaultHttpClient();

    HttpParams httpParams = mHttpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, Huaban.TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, Huaban.TIMEOUT);
    HttpProtocolParams.setUserAgent(httpParams, Huaban.USER_AGENT);

    mListener.onStart();
}
 
Example 14
Source File: WebServiceClient.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private WebServiceClient(Context context)
{
    g = new DefaultHttpClient();
    HttpParams httpparams = g.getParams();
    httpparams.setIntParameter("http.socket.timeout", 60000);
    httpparams.setIntParameter("http.connection.timeout", 60000);
    h = context;
}
 
Example 15
Source File: RESTClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example 16
Source File: SOAPClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example 17
Source File: RestProtocol.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
        if (connectionMonitor == null) {
            connectionMonitor = new ConnectionMonitor();
        }

        // TODO more configs to add

        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        // 20 is the default maxTotal of current PoolingClientConnectionManager
        connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
        connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));

        connectionMonitor.addConnectionManager(connectionManager);

//        BasicHttpContext localContext = new BasicHttpContext();

        DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

        httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        return Long.parseLong(value) * 1000;
                    }
                }
                // TODO constant
                return 30 * 1000;
            }
        });

        HttpParams params = httpClient.getParams();
        // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
        HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setSoKeepalive(params, true);

        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);

        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        clients.add(client);

        client.register(RpcContextFilter.class);
        for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
            if (!StringUtils.isEmpty(clazz)) {
                try {
                    client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
                } catch (ClassNotFoundException e) {
                    throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
                }
            }
        }

        // TODO protocol
        ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
        return target.proxy(serviceType);
    }
 
Example 18
Source File: RestProtocol.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
        if (connectionMonitor == null) {
            connectionMonitor = new ConnectionMonitor();
        }

        // TODO more configs to add

        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        // 20 is the default maxTotal of current PoolingClientConnectionManager
        connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
        connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));

        connectionMonitor.addConnectionManager(connectionManager);

//        BasicHttpContext localContext = new BasicHttpContext();

        DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

        httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        return Long.parseLong(value) * 1000;
                    }
                }
                // TODO constant
                return 30 * 1000;
            }
        });

        HttpParams params = httpClient.getParams();
        // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
        HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setSoKeepalive(params, true);

        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);

        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        clients.add(client);

        client.register(RpcContextFilter.class);
        for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
            if (!StringUtils.isEmpty(clazz)) {
                try {
                    client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
                } catch (ClassNotFoundException e) {
                    throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
                }
            }
        }

        // TODO protocol
        ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
        return target.proxy(serviceType);
    }
 
Example 19
Source File: RestProtocol.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
        if (connectionMonitor == null) {
            connectionMonitor = new ConnectionMonitor();
        }

        // TODO more configs to add

        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        // 20 is the default maxTotal of current PoolingClientConnectionManager
        connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
        connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));

        connectionMonitor.addConnectionManager(connectionManager);

//        BasicHttpContext localContext = new BasicHttpContext();

        DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

        httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        return Long.parseLong(value) * 1000;
                    }
                }
                // TODO constant
                return 30 * 1000;
            }
        });

        HttpParams params = httpClient.getParams();
        // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
        HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setSoKeepalive(params, true);

        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);

        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        clients.add(client);

        client.register(RpcContextFilter.class);
        for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
            if (!StringUtils.isEmpty(clazz)) {
                try {
                    client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
                } catch (ClassNotFoundException e) {
                    throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
                }
            }
        }

        // TODO protocol
        ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
        return target.proxy(serviceType);
    }
 
Example 20
Source File: RestProtocol.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
        if (connectionMonitor == null) {
            connectionMonitor = new ConnectionMonitor();
        }

        // TODO more configs to add

        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        // 20 is the default maxTotal of current PoolingClientConnectionManager
        connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
        connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));

        connectionMonitor.addConnectionManager(connectionManager);

//        BasicHttpContext localContext = new BasicHttpContext();

        DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

        httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        return Long.parseLong(value) * 1000;
                    }
                }
                // TODO constant
                return 30 * 1000;
            }
        });

        HttpParams params = httpClient.getParams();
        // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
        HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setSoKeepalive(params, true);

        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);

        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        clients.add(client);

        client.register(RpcContextFilter.class);
        for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
            if (!StringUtils.isEmpty(clazz)) {
                try {
                    client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
                } catch (ClassNotFoundException e) {
                    throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
                }
            }
        }

        // TODO protocol
        ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
        return target.proxy(serviceType);
    }