Example usage for org.apache.commons.httpclient HttpClient HttpClient

List of usage examples for org.apache.commons.httpclient HttpClient HttpClient

Introduction

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

Prototype

public HttpClient(HttpClientParams paramHttpClientParams) 

Source Link

Usage

From source file:experiments.NewClass.java

/**
 * DOCUMENT ME!/*from  ww w  . j a  v  a2  s  . c  o m*/
 *
 * @param  args  DOCUMENT ME!
 */
public static void main(final String[] args) {
    final HttpClient h = new HttpClient(new MultiThreadedHttpConnectionManager());
    System.out.println("h was created: " + h);
}

From source file:MultiThreadedExample.java

public static void main(String[] args) {

    // Create an HttpClient with the MultiThreadedHttpConnectionManager.
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    // Set the default host/protocol for the methods to connect to.
    // This value will only be used if the methods are not given an absolute URI
    httpClient.getHostConfiguration().setHost("jakarta.apache.org", 80, "http");

    // create an array of URIs to perform GETs on
    String[] urisToGet = { "/", "/commons/", "/commons/httpclient/",
            "http://svn.apache.org/viewvc/jakarta/httpcomponents/oac.hc3x/" };

    // create a thread for each URI
    GetThread[] threads = new GetThread[urisToGet.length];
    for (int i = 0; i < threads.length; i++) {
        GetMethod get = new GetMethod(urisToGet[i]);
        get.setFollowRedirects(true);/*from www.  ja  v  a2 s .  co  m*/
        threads[i] = new GetThread(httpClient, get, i + 1);
    }

    // start the threads
    for (int j = 0; j < threads.length; j++) {
        threads[j].start();
    }

}

From source file:fr.cls.atoll.motu.library.misc.cas.HttpClientTutorial.java

public static void main(String[] args) {

    // test();/* w  w  w.  j a va2  s . c om*/
    // System.setProperty("proxyHost", "proxy.cls.fr"); // adresse IP
    // System.setProperty("proxyPort", "8080");
    // System.setProperty("socksProxyPort", "1080");
    //
    // Authenticator.setDefault(new MyAuthenticator());

    // Create an instance of HttpClient.
    // HttpClient client = new HttpClient();
    HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
    // Create a method instance.
    GetMethod method = new GetMethod(url);

    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setProxy("proxy.cls.fr", 8080);
    client.setHostConfiguration(hostConfiguration);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    // String username = "xxx";
    // String password = "xxx";
    // Credentials credentials = new UsernamePasswordCredentials(username, password);
    // AuthScope authScope = new AuthScope("proxy.cls.fr", 8080);
    //     
    // client.getState().setProxyCredentials(authScope, credentials);

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        System.out.println(new String(responseBody));

    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:integration.HeavyLoad.java

public static void main(String[] args) throws IOException {
    url = args[0] + "/create.json";
    final String specFile = args[1];

    for (int i = 2; i < args.length; i++) {
        String server = args[i];/*from  w  ww  .j  ava 2s.  com*/
        servers.add(new ServerStats(server));
    }
    if (servers.isEmpty()) {
        servers.add(new ServerStats(DEFAULT));
    }

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    final HttpConnectionManagerParams params = connectionManager.getParams();
    params.setSoTimeout(TIMEOUT);
    params.setConnectionTimeout(TIMEOUT);
    params.setDefaultMaxConnectionsPerHost(NB_THREADS);
    params.setMaxTotalConnections(NB_THREADS);

    httpClient = new HttpClient(connectionManager);

    spec = FileUtilities.readWholeTextFile(new File(specFile));

    Thread[] threads = new Thread[NB_THREADS];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread(new Reader(), "reader-" + i);
        threads[i].start();
    }
    for (int i = 0; i < threads.length; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
        }
    }
}

From source file:com.jivesoftware.os.jive.utils.shell.utils.Curl.java

public static Curl create() {
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient client = new HttpClient(connectionManager);

    return new Curl(client);
}

From source file:com.carrotsearch.util.httpclient.HttpClientFactory.java

/**
 * @param timeout Timeout in milliseconds.
 * @return Returns a client with sockets configured to timeout after some sensible
 *         time.//from   w w w  .j a  v a2s .c o m
 */
public static HttpClient getTimeoutingClient(int timeout) {
    final HttpClient httpClient = new HttpClient(new SingleHttpConnectionManager());

    configureProxy(httpClient);

    // Setup default timeouts.
    httpClient.getParams().setSoTimeout(timeout);
    httpClient.getParams().setIntParameter("http.connection.timeout", timeout);

    // Not important (single http connection manager), but anyway.
    httpClient.getParams().setConnectionManagerTimeout(timeout);

    return httpClient;
}

From source file:com.cloud.utils.net.HTTPUtils.java

public static HttpClient getHTTPClient() {
    return new HttpClient(s_httpClientManager);
}

From source file:com.simplifide.core.net.LicenseConnection.java

public void connect() {
    HttpClientParams params = new HttpClientParams();
    params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    params.setIntParameter(HttpClientParams.MAX_REDIRECTS, 4);

    HttpClient client = new HttpClient(params);
    client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

    GetMethod post = new GetMethod("http://simplifide.com/drupal/free_trial2");
    post.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    /* NameValuePair[] data = {
        new NameValuePair("user_name", "joe"),
        new NameValuePair("password", "aaa"),
        new NameValuePair("name", "joker"),
        new NameValuePair("email", "beta"),
      };/* www  .  j a v  a  2s  . c om*/
      post.setRequestBody(data);
      */
    try {
        int rettype = client.executeMethod(post);

        byte[] responseBody = post.getResponseBody();

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        String tstring = new String(responseBody);

    } catch (HttpException e) {

        HardwareLog.logError(e);
    } catch (IOException e) {

        HardwareLog.logError(e);
    } finally {
        post.releaseConnection();
    }

}

From source file:com.maltera.mylyn.internal.rt.core.restv1.RtClient.java

public RtClient(AbstractWebLocation location) {
    this.location = location;

    this.httpClient = new HttpClient(WebUtil.getConnectionManager());
    WebUtil.configureHttpClient(this.httpClient, USER_AGENT);
}

From source file:de.loercher.localpress.connector.HttpClientJob.java

@Override
public void run() {
    try {//from  w  w w.j  a va  2s.c o  m
        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        HttpClient client = new HttpClient(connectionManager);
        HttpMethod method = new GetMethod(url);
        client.executeMethod(method);

        System.out.println(new String(method.getResponseBody()));

    } catch (IOException ex) {
        Logger.getLogger(HttpClientJob.class.getName()).log(Level.SEVERE, null, ex);
    }
}