Example usage for org.apache.commons.httpclient SimpleHttpConnectionManager setParams

List of usage examples for org.apache.commons.httpclient SimpleHttpConnectionManager setParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient SimpleHttpConnectionManager setParams.

Prototype

public void setParams(HttpConnectionManagerParams paramHttpConnectionManagerParams) 

Source Link

Usage

From source file:net.sourceforge.jcctray.model.HTTPCruise.java

private static HttpClient getClient(Host host) {
    SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    HttpConnectionManagerParams connParams = new HttpConnectionManagerParams();
    connParams.setConnectionTimeout(JCCTraySettings.getInstance().getInt(ISettingsConstants.HTTP_TIMEOUT));
    connParams.setSoTimeout(JCCTraySettings.getInstance().getInt(ISettingsConstants.HTTP_TIMEOUT));
    connectionManager.setParams(connParams);
    HttpClient client = new HttpClient(connectionManager);

    client.getParams().setAuthenticationPreemptive(true);
    if (!StringUtils.isEmptyOrNull(host.getHostName()) && !StringUtils.isEmptyOrNull(host.getPassword())) {
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(host.getUsername(),
                host.getPassword());//from w  w w. j  a v a  2s .  c o  m
        client.getState().setCredentials(new AuthScope(null, -1, null, null), credentials);
    }

    return client;
}

From source file:edu.utah.further.core.ws.HttpUtil.java

/**
 * Open an HTTP GET method connection to a URL and read the returned response into an
 * object./*www.j a  v  a  2 s .co m*/
 * 
 * @param <T>
 *            return type
 * @param url
 *            remote URL (usually a web service's URL)
 * @param timeoutSecs
 *            HTTP socket connection timeout [seconds]
 * @return <code>HttpClient</code> {@link HttpMethod} transfer object, containing the
 *         response headers and body
 * @throws WsException
 *             if an error as occurred either in transport or protocol
 */
public static HttpResponseTo getHttpGetResponseBody(final String url, final int timeoutSecs)
        throws WsException {
    // Create an instance of HttpClient with appropriate parameters
    final SimpleHttpConnectionManager cm = new SimpleHttpConnectionManager();
    final HttpConnectionManagerParams params = cm.getParams();
    params.setConnectionTimeout(timeoutSecs * 1000);
    params.setSoTimeout(timeoutSecs * 1000);
    cm.setParams(params);
    final HttpClient client = new HttpClient(cm);

    // Create a method instance
    final GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(NUMBER_HTTP_REQUEST_TRIALS, false));
    try {
        // Execute the method
        /* final int statusCode = */client.executeMethod(method);
        // if (statusCode != HttpStatus.SC_OK)
        // {
        // log.error("Method failed: " + method.getStatusLine());
        // }
        return new HttpResponseTo(method);
    } catch (final HttpException e) {
        // HTTP protocol violation (e.g. bad method?!)
        throw new WsException(PROTOCOL_VIOLATION, e.getMessage());
    } catch (final IOException e) {
        // Transport error. Could be an invalid URL
        throw new WsException(TRANSPORT_ERROR, e.getMessage());
    } catch (final Throwable e) {
        // Exceptions
        throw new WsException(SERVER_ERROR, e.getMessage());
    } finally {
        // Important: release the connection
        method.releaseConnection();
    }
}

From source file:cn.leancloud.diamond.client.processor.ServerAddressProcessor.java

private void initHttpClient() {
    HostConfiguration hostConfiguration = new HostConfiguration();

    SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    connectionManager.closeIdleConnections(5000L);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    connectionManager.setParams(params);

    configHttpClient = new HttpClient(connectionManager);
    configHttpClient.setHostConfiguration(hostConfiguration);
}