Package org.apache.http.client

Examples of org.apache.http.client.HttpState


        return new DefaultAuthenticationHandler();
    }


    protected HttpState createHttpState() {
        return new HttpState();
    }
View Full Code Here


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

        // Create a local instance of HttpState
        HttpState localState = new HttpState();
       
        // Obtain default HTTP context
        HttpContext defaultContext = httpclient.getDefaultContext();
        // Create local HTTP context
        HttpContext localContext = new HttpClientContext(defaultContext);
        // Bind custom HTTP state to the local context
        localContext.setAttribute(HttpClientContext.HTTP_STATE, localState);
       
        HttpGet httpget = new HttpGet("http://www.google.com/");

        System.out.println("executing request " + httpget.getURI());

        // Pass local context as a parameter
        HttpResponse response = httpclient.execute(httpget, localContext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            System.out.println("Chunked?: " + entity.isChunked());
        }
        Cookie[] cookies = localState.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            System.out.println("Local cookie: " + cookies[i]);
        }
       
        // Consume response content
View Full Code Here

            if (status < 200) {
                throw new HttpException("Unexpected response to CONNECT request: " +
                        response.getStatusLine());
            }
           
            HttpState state = (HttpState) context.getAttribute(HttpClientContext.HTTP_STATE);
           
            if (state != null && HttpClientParams.isAuthenticating(params)) {
                if (this.authHandler.isProxyAuthenticationRequested(response, context)) {

                    LOG.debug("Proxy requested authentication");
View Full Code Here

            }
           
            return new RoutedRequest.Impl(redirect, newRoute);
        }

        HttpState state = (HttpState) context.getAttribute(HttpClientContext.HTTP_STATE);
       
        if (state != null && HttpClientParams.isAuthenticating(params)) {

            if (this.authHandler.isTargetAuthenticationRequested(response, context)) {
View Full Code Here

        if (context == null) {
            throw new IllegalArgumentException("HTTP context may not be null");
        }
       
        // Obtain HTTP state
        HttpState state = (HttpState) context.getAttribute(
                HttpClientContext.HTTP_STATE);
        if (state == null) {
            LOG.info("HTTP state not available in HTTP context");
            return;
        }
       
        // Obtain the registry of cookie specs
        CookieSpecRegistry registry= (CookieSpecRegistry) context.getAttribute(
                HttpClientContext.COOKIESPEC_REGISTRY);
        if (registry == null) {
            LOG.info("CookieSpec registry not available in HTTP context");
            return;
        }
       
        // Obtain the target host (required)
        HttpHost targetHost = (HttpHost) context.getAttribute(
                HttpExecutionContext.HTTP_TARGET_HOST);
        if (targetHost == null) {
            throw new IllegalStateException("Target host not specified in HTTP context");
        }
       
        // Obtain the client connection (required)
        ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute(
                HttpExecutionContext.HTTP_CONNECTION);
        if (conn == null) {
            throw new IllegalStateException("Client connection not specified in HTTP context");
        }

        String policy = HttpClientParams.getCookiePolicy(request.getParams());
        if (LOG.isDebugEnabled()) {
            LOG.debug("CookieSpec selected: " + policy);
        }
       
        URI requestURI;
        if (request instanceof HttpUriRequest) {
            requestURI = ((HttpUriRequest) request).getURI();
        } else {
            try {
                requestURI = new URI(request.getRequestLine().getUri());
            } catch (URISyntaxException ex) {
                throw new ProtocolException("Invalid request URI: " +
                        request.getRequestLine().getUri(), ex);
            }
        }
       
        String hostName = targetHost.getHostName();
        int port = targetHost.getPort();
        if (port < 0) {
            port = conn.getRemotePort();
        }
       
        CookieOrigin cookieOrigin = new CookieOrigin(
                hostName,
                port,
                requestURI.getPath(),
                conn.isSecure());
       
        // Get an instance of the selected cookie policy
        CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
        // Get all cookies available in the HTTP state
        Cookie[] cookies = state.getCookies();
        // Find cookies matching the given origin
        List matchedCookies = new ArrayList(cookies.length);
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookieSpec.match(cookie, cookieOrigin)) {
View Full Code Here

        if (context == null) {
            throw new IllegalArgumentException("HTTP context may not be null");
        }
       
        // Obtain HTTP state
        HttpState state = (HttpState) context.getAttribute(
                HttpClientContext.HTTP_STATE);
        if (state == null) {
            LOG.info("HTTP state not available in HTTP context");
            return;
        }
View Full Code Here

TOP

Related Classes of org.apache.http.client.HttpState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.