hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christophe Bouhier (MC/ECM)" <Christophe.Bouh...@ericsson.com>
Subject newbie authentication.
Date Mon, 07 Feb 2005 10:39:31 GMT
Hello, 

I am trying to implement authentication and played around with
the examples. The Method is a HEAD method, I first execute without 
authentication and if the result is a 401, I set credentials and setDoAuthentication and
execute again. On this however I get a Connection Timeout exception. (I get the impression
that
the proxy settting is lost in the Header???)

Propably a newbie issue, but I haven't  been able to pinpoint. 

Can someone help?

here is code: (See the code which checks for the 401/SC_UNAUTHORIZED status).

public static ipNetHEADInfo getHeadInfo(URL url) throws ipException {
        ipNetHEADInfo head = null;
        HttpClient hClient = new HttpClient();
        hClient.getParams().setSoTimeout(5000);
        //    hClient.setTimeout(5000); deprecated in 3.0 client
        HostConfiguration hc = hClient.getHostConfiguration();
        hc = setProxySetttings(hClient, hc);
        HeadMethod hMethod = null;
        try {
            hMethod = new HeadMethod(url.toString());
            hMethod.setRequestHeader("cache-control", "no-cache");
            int status = hClient.executeMethod(hMethod);

            if (status != HttpStatus.SC_OK) {
                // Check for redirection.
                if (status == HttpStatus.SC_MOVED_PERMANENTLY
                        || status == HttpStatus.SC_MOVED_TEMPORARILY
                        || status == HttpStatus.SC_SEE_OTHER
                        || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
                    String redirectLocation;
                    Header locationHeader = hMethod
                            .getResponseHeader("location");
                    if (locationHeader != null) {
                        redirectLocation = locationHeader.getValue();
                        hMethod = new HeadMethod(redirectLocation);
                        status = hClient.executeMethod(hMethod);
                        if (status != HttpStatus.SC_OK) {
                            throw new ipException(hMethod.getStatusLine()
                                    .getReasonPhrase());
                        }
                    } else {
                        // The response is invalid and did not provide the new
                        // location for
                        // the resource. Report an error or possibly handle the
                        // response
                        // like a 404 Not Found error.
                    }
                } else {
                    if (status == HttpStatus.SC_UNAUTHORIZED) {
                        // Retry with password.                        
                        hClient.getState().setCredentials(
                                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                                new UsernamePasswordCredentials("username", "password")
                            );
                        hMethod = new HeadMethod(url.toString());
                        hMethod.setDoAuthentication(true);
                        status = hClient.executeMethod(hMethod);
                        if (status != HttpStatus.SC_OK) {
                            throw new ipException(hMethod.getStatusLine()
                                    .getReasonPhrase());
                        }
                    }
                    throw new ipException(hMethod.getStatusLine()
                            .getReasonPhrase());
                
            }
            head = getHeadInfo(hMethod);
        } catch (HttpException he) {
            // Received Illegal redirect exception on some URLs
            throw new ipException(he.getMessage());
        } catch (IOException ioe) {
            throw new ipException(ioe.getMessage());
        } catch (java.lang.IllegalArgumentException iae) {
            throw new ipException(iae.getMessage());
        } finally {
            if (hMethod != null) {
                hMethod.releaseConnection();
            }
        }
        return head;
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message