hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: newbie authentication.
Date Tue, 08 Feb 2005 22:19:11 GMT
Christophe,

What web server are you targeting? Do I understand it right that you are
using a proxy to communicate with the target server? If yes, what kind
of proxy is that?

Please also take a look at the HttpClient troubleshooting guide
<http://jakarta.apache.org/commons/httpclient/3.0/troubleshooting.html> 

If still unable to pinpoint the problem please post the wire/context log
of the HTTP session to this list

Oleg

On Mon, 2005-02-07 at 18:39 +0800, Christophe Bouhier (MC/ECM) wrote:
> 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
> 


---------------------------------------------------------------------
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