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

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

Introduction

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

Prototype

public HttpClientParams getParams() 

Source Link

Usage

From source file:BasicAuthenticationExecuteJSPMethod.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("parameterKey", "value");

    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));

    GetMethod method = new GetMethod("/commons/folder/protected.jsp");
    try {//from  w  w w .jav  a  2  s . c o m
        client.executeMethod(host, method);
        System.err.println(method.getStatusLine());
        System.err.println(method.getResponseBodyAsString());
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:HttpClientParameter.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    HostConfiguration host = client.getHostConfiguration();
    host.setHost("www.google.com");

    GetMethod method = new GetMethod("http://www.yahoo.com");

    int returnCode = client.executeMethod(host, method);

    System.err.println(method.getResponseBodyAsString());

    System.err//ww  w  .j av  a2  s. c  om
            .println("User-Agent: " + method.getHostConfiguration().getParams().getParameter("http.useragent"));

    System.err.println("User-Agent: " + method.getParams().getParameter("http.useragent"));

    method.releaseConnection();
}

From source file:OptionsMethodExample.java

public static void main(String args[]) {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");

    OptionsMethod method = new OptionsMethod("http://www.google.com");

    try {//from w w  w  .j  av a2  s . c  o  m
        int returnCode = client.executeMethod(method);

        Enumeration list = method.getAllowedMethods();

        while (list.hasMoreElements()) {
            System.err.println(list.nextElement());
        }

    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }

}

From source file:GetCookiePrintAndSetValue.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    GetMethod method = new GetMethod("http://localhost:8080/");
    try {/*from  ww  w.ja  va  2s.  c  o m*/
        client.executeMethod(method);
        Cookie[] cookies = client.getState().getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            System.err.println("Cookie: " + cookie.getName() + ", Value: " + cookie.getValue()
                    + ", IsPersistent?: " + cookie.isPersistent() + ", Expiry Date: " + cookie.getExpiryDate()
                    + ", Comment: " + cookie.getComment());

            cookie.setValue("My own value");
        }
        client.executeMethod(method);
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:BasicAuthenticationForJSPPage.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));

    Credentials credentials = new UsernamePasswordCredentials("tomcat", "tomcat");
    AuthScope authScope = new AuthScope(host.getHost(), host.getPort());
    HttpState state = client.getState();
    state.setCredentials(authScope, credentials);

    GetMethod method = new GetMethod("/commons/chapter01/protected.jsp");
    try {/*from  w ww  . j  ava 2  s .c o  m*/
        client.executeMethod(host, method);
        System.err.println(method.getStatusLine());
        System.err.println(method.getResponseBodyAsString());
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:UsingHttpClientInsideThread.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");

    HostConfiguration host = new HostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));

    // first Get a big file
    MethodThread bigDataThread = new MethodThread(client, host, "/big_movie.wmv");

    bigDataThread.start();//from w ww  .j  a  v a 2  s.c  om

    // next try and get a small file
    MethodThread normalThread = new MethodThread(client, host, "/");

    normalThread.start();
}

From source file:GetMethodExample.java

public static void main(String args[]) {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");
    client.getParams().setParameter("http.connection.timeout", new Integer(5000));

    GetMethod method = new GetMethod();
    FileOutputStream fos = null;//from w w w .  j  av  a  2s  .c o  m

    try {

        method.setURI(new URI("http://www.google.com", true));
        int returnCode = client.executeMethod(method);

        if (returnCode != HttpStatus.SC_OK) {
            System.err.println("Unable to fetch default page, status code: " + returnCode);
        }

        System.err.println(method.getResponseBodyAsString());

        method.setURI(new URI("http://www.google.com/images/logo.gif", true));
        returnCode = client.executeMethod(method);

        if (returnCode != HttpStatus.SC_OK) {
            System.err.println("Unable to fetch image, status code: " + returnCode);
        }

        byte[] imageData = method.getResponseBody();
        fos = new FileOutputStream(new File("google.gif"));
        fos.write(imageData);

        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setHost("www.yahoo.com", null, 80, Protocol.getProtocol("http"));

        method.setURI(new URI("/", true));

        client.executeMethod(hostConfig, method);

        System.err.println(method.getResponseBodyAsString());

    } catch (HttpException he) {
        System.err.println(he);
    } catch (IOException ie) {
        System.err.println(ie);
    } finally {
        method.releaseConnection();
        if (fos != null)
            try {
                fos.close();
            } catch (Exception fe) {
            }
    }

}

From source file:HttpClientPreferences.java

public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();

    System.err.println(//from  w w w .ja  v  a 2 s  .com
            "The User Agent before changing it is: " + client.getParams().getParameter("http.useragent"));

    client.getParams().setParameter("http.useragent", "Browser at Client level");

    System.err.println("Client's User Agent is: " + client.getParams().getParameter("http.useragent"));

    GetMethod method = new GetMethod("http://www.google.com");
    method.getParams().setParameter("http.useragent", "Browser at Method level");
    try {
        client.executeMethod(method);
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
    System.err.println("Method's User Agent is: " + method.getParams().getParameter("http.useragent"));

}

From source file:com.discursive.jccook.httpclient.BasicAuthExample.java

public static void main(String[] args) throws HttpException, IOException {
    // Configure Logging
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");

    HttpClient client = new HttpClient();
    HttpState state = client.getState();
    HttpClientParams params = client.getParams();

    // Set credentials on the client
    Credentials credentials = new UsernamePasswordCredentials("testuser", "crazypass");
    state.setCredentials(null, null, credentials);
    params.setAuthenticationPreemptive(true);

    String url = "http://www.discursive.com/jccook/auth/";
    HttpMethod method = new GetMethod(url);

    client.executeMethod(method);//from w w w  .j a v  a  2  s.  c om
    String response = method.getResponseBodyAsString();

    System.out.println(response);
    method.releaseConnection();
}

From source file:FormLoginDemo.java

public static void main(String[] args) throws Exception {

    HttpClient client = new HttpClient();
    client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    // 'developer.java.sun.com' has cookie compliance problems
    // Their session cookie's domain attribute is in violation of the RFC2109
    // We have to resort to using compatibility cookie policy

    GetMethod authget = new GetMethod("/servlet/SessionServlet");

    client.executeMethod(authget);/*from  w  w  w  .  j a  va2 s .  co m*/
    System.out.println("Login form get: " + authget.getStatusLine().toString());
    // release any connection resources used by the method
    authget.releaseConnection();
    // See if we got any cookies
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] initcookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
    System.out.println("Initial set of cookies:");
    if (initcookies.length == 0) {
        System.out.println("None");
    } else {
        for (int i = 0; i < initcookies.length; i++) {
            System.out.println("- " + initcookies[i].toString());
        }
    }

    PostMethod authpost = new PostMethod("/servlet/SessionServlet");
    // Prepare login parameters
    NameValuePair action = new NameValuePair("action", "login");
    NameValuePair url = new NameValuePair("url", "/index.html");
    NameValuePair userid = new NameValuePair("UserId", "userid");
    NameValuePair password = new NameValuePair("Password", "password");
    authpost.setRequestBody(new NameValuePair[] { action, url, userid, password });

    client.executeMethod(authpost);
    System.out.println("Login form post: " + authpost.getStatusLine().toString());
    // release any connection resources used by the method
    authpost.releaseConnection();
    // See if we got any cookies
    // The only way of telling whether logon succeeded is 
    // by finding a session cookie
    Cookie[] logoncookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false,
            client.getState().getCookies());
    System.out.println("Logon cookies:");
    if (logoncookies.length == 0) {
        System.out.println("None");
    } else {
        for (int i = 0; i < logoncookies.length; i++) {
            System.out.println("- " + logoncookies[i].toString());
        }
    }
    // Usually a successful form-based login results in a redicrect to 
    // another url
    int statuscode = authpost.getStatusCode();
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
            || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
        Header header = authpost.getResponseHeader("location");
        if (header != null) {
            String newuri = header.getValue();
            if ((newuri == null) || (newuri.equals(""))) {
                newuri = "/";
            }
            System.out.println("Redirect target: " + newuri);
            GetMethod redirect = new GetMethod(newuri);

            client.executeMethod(redirect);
            System.out.println("Redirect: " + redirect.getStatusLine().toString());
            // release any connection resources used by the method
            redirect.releaseConnection();
        } else {
            System.out.println("Invalid redirect");
            System.exit(1);
        }
    }
}