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

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

Introduction

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

Prototype

HttpState

Source Link

Usage

From source file:CookieDemoApp.java

/**
 *
 * Usage:/*from w  ww. ja va 2s  . c  om*/
 *          java CookieDemoApp http://mywebserver:80/
 *
 *  @param args command line arguments
 *                 Argument 0 is a URL to a web server
 *
 *
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Usage: java CookieDemoApp <url>");
        System.err.println("<url> The url of a webpage");
        System.exit(1);
    }
    // Get target URL
    String strURL = args[0];
    System.out.println("Target URL: " + strURL);

    // Get initial state object
    HttpState initialState = new HttpState();
    // Initial set of cookies can be retrieved from persistent storage and 
    // re-created, using a persistence mechanism of choice,
    Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false);
    // and then added to your HTTP state instance
    initialState.addCookie(mycookie);

    // Get HTTP client instance
    HttpClient httpclient = new HttpClient();
    httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    httpclient.setState(initialState);

    // RFC 2101 cookie management spec is used per default
    // to parse, validate, format & match cookies
    httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    // A different cookie management spec can be selected
    // when desired

    //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);
    // Netscape Cookie Draft spec is provided for completeness
    // You would hardly want to use this spec in real life situations
    //httppclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    // Compatibility policy is provided in order to mimic cookie
    // management of popular web browsers that is in some areas 
    // not 100% standards compliant

    // Get HTTP GET method
    GetMethod httpget = new GetMethod(strURL);
    // Execute HTTP GET
    int result = httpclient.executeMethod(httpget);
    // Display status code
    System.out.println("Response status code: " + result);
    // Get all the cookies
    Cookie[] cookies = httpclient.getState().getCookies();
    // Display the cookies
    System.out.println("Present cookies: ");
    for (int i = 0; i < cookies.length; i++) {
        System.out.println(" - " + cookies[i].toExternalForm());
    }
    // Release current connection to the connection pool once you are done
    httpget.releaseConnection();
}

From source file:com.dtolabs.client.utils.ClientState.java

private static HttpState getState(Thread t) {
    if (httpStates.containsKey(t)) {
        return (HttpState) httpStates.get(t);
    } else {//  w  ww  .  j  a  va2s.  c  o m
        HttpState hs = new HttpState();
        setState(t, hs);
        return hs;
    }
}

From source file:net.sf.antcontrib.net.httpclient.HttpStateType.java

public HttpStateType(Project p) {
    super();
    setProject(p);

    state = new HttpState();
}

From source file:com.zimbra.common.httpclient.HttpClientUtil.java

public static int executeMethod(HttpClient client, HttpMethod method, HttpState state)
        throws HttpException, IOException {
    ProxyHostConfiguration proxyConfig = HttpProxyConfig.getProxyConfig(client.getHostConfiguration(),
            method.getURI().toString());
    if (proxyConfig != null && proxyConfig.getUsername() != null && proxyConfig.getPassword() != null) {
        if (state == null) {
            state = client.getState();// www . ja  v  a  2 s .c  om
            if (state == null) {
                state = new HttpState();
            }
        }
        state.setProxyCredentials(new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()),
                new UsernamePasswordCredentials(proxyConfig.getUsername(), proxyConfig.getPassword()));
    }
    return client.executeMethod(proxyConfig, method, state);
}

From source file:com.cellbots.communication.AppEngineCommChannel.java

public AppEngineCommChannel(String name, String cmdUrl, CommMessageListener listener) {
    super(name);/*from w  w w. java 2  s. c o  m*/
    mMessageListener = listener;
    mHttpCmdUrl = cmdUrl;
    stopReading = false;
    mHttpState = new HttpState();
}

From source file:arena.httpclient.commons.JakartaCommonsHttpClient.java

public JakartaCommonsHttpClient(HttpConnectionManager manager) {
    this.manager = manager;
    this.state = new HttpState();
}

From source file:com.gargoylesoftware.htmlunit.CookieManagerTest.java

/**
 * Verifies the basic cookie manager behavior.
 * @throws Exception if an error occurs/*from   ww  w  . j av a2s  .co m*/
 */
@Test
public void basicBehavior() throws Exception {
    // Create a new cookie manager.
    final CookieManager mgr = new CookieManager();
    assertTrue(mgr.isCookiesEnabled());
    assertTrue(mgr.getCookies().isEmpty());

    // Add a cookie to the manager.
    final Cookie cookie = new Cookie("a", "b");
    mgr.addCookie(cookie);
    assertFalse(mgr.getCookies().isEmpty());

    // Update an HTTP state.
    final HttpState state = new HttpState();
    mgr.updateState(state);
    assertEquals(1, state.getCookies().length);

    // Remove the cookie from the manager.
    mgr.removeCookie(cookie);
    assertTrue(mgr.getCookies().isEmpty());

    // Update an HTTP state after removing the cookie.
    mgr.updateState(state);
    assertEquals(0, state.getCookies().length);

    // Add the cookie back to the manager.
    mgr.addCookie(cookie);
    assertFalse(mgr.getCookies().isEmpty());

    // Update an HTTP state after adding the cookie back to the manager.
    mgr.updateState(state);
    assertEquals(1, state.getCookies().length);

    // Clear all cookies from the manager.
    mgr.clearCookies();
    assertTrue(mgr.getCookies().isEmpty());

    // Update an HTTP state after clearing all cookies from the manager.
    mgr.updateState(state);
    assertEquals(0, state.getCookies().length);

    // Disable cookies.
    mgr.setCookiesEnabled(false);
    assertFalse(mgr.isCookiesEnabled());

    // Add a cookie after disabling cookies.
    mgr.addCookie(cookie);
    assertFalse(mgr.getCookies().isEmpty());

    // Update an HTTP state after adding a cookie while cookies are disabled.
    mgr.updateState(state);
    assertEquals(0, state.getCookies().length);

    // Enable cookies again.
    mgr.setCookiesEnabled(true);
    assertTrue(mgr.isCookiesEnabled());

    // Update an HTTP state after enabling cookies again.
    mgr.updateState(state);
    assertEquals(1, state.getCookies().length);

    // Update the manager with a new state.
    final Cookie cookie2 = new Cookie("x", "y");
    final HttpState state2 = new HttpState();
    state2.addCookie(cookie2.toHttpClient());
    mgr.updateFromState(state2);
    assertEquals(1, mgr.getCookies().size());
    assertEquals(cookie2, mgr.getCookies().iterator().next());
}

From source file:com.lazerycode.ebselen.customhandlers.FileDownloader.java

/**
 * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie state
 *
 * @param seleniumCookieSet//from  w  ww .  j ava  2s .c o m
 * @return
 */
private HttpState mimicCookieState(Set<org.openqa.selenium.Cookie> seleniumCookieSet) {
    HttpState mimicWebDriverCookieState = new HttpState();
    for (org.openqa.selenium.Cookie seleniumCookie : seleniumCookieSet) {
        Cookie httpClientCookie = new Cookie(seleniumCookie.getDomain(), seleniumCookie.getName(),
                seleniumCookie.getValue(), seleniumCookie.getPath(), seleniumCookie.getExpiry(),
                seleniumCookie.isSecure());
        mimicWebDriverCookieState.addCookie(httpClientCookie);
    }
    return mimicWebDriverCookieState;
}

From source file:com.zimbra.common.httpclient.HttpClientUtil.java

public static HttpState newHttpState(ZAuthToken authToken, String host, boolean isAdmin) {
    HttpState state = new HttpState();
    if (authToken != null) {
        Map<String, String> cookieMap = authToken.cookieMap(isAdmin);
        if (cookieMap != null) {
            for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
                state.addCookie(new Cookie(host, ck.getKey(), ck.getValue(), "/", null, false));
            }/*from   www  .  j  a  v a2s  .c o  m*/
        }
    }
    return state;
}

From source file:com.twinsoft.convertigo.engine.billing.GoogleAnalyticsTicketManager.java

private HttpClient prepareHttpClient(String[] url) throws EngineException, MalformedURLException {
    final Pattern scheme_host_pattern = Pattern.compile("https://(.*?)(?::([\\d]*))?(/.*|$)");

    HttpClient client = new HttpClient();
    HostConfiguration hostConfiguration = client.getHostConfiguration();
    HttpState httpState = new HttpState();
    client.setState(httpState);//from  ww  w.  j  av  a 2 s .c o m
    if (proxyManager != null) {
        proxyManager.getEngineProperties();
        proxyManager.setProxy(hostConfiguration, httpState, new URL(url[0]));
    }

    Matcher matcher = scheme_host_pattern.matcher(url[0]);
    if (matcher.matches()) {
        String host = matcher.group(1);
        String sPort = matcher.group(2);
        int port = 443;

        try {
            port = Integer.parseInt(sPort);
        } catch (Exception e) {
        }

        try {
            Protocol myhttps = new Protocol("https",
                    MySSLSocketFactory.getSSLSocketFactory(null, null, null, null, true), port);

            hostConfiguration.setHost(host, port, myhttps);
            url[0] = matcher.group(3);
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
        }
    }
    return client;
}