You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Ramit Bhardwaj <ra...@cybage.com> on 2005/09/26 12:25:53 UTC

urgent: Problem using basic authentication

Hi guys,
I am using the sample application given on the commons httpclient site for basic authentication. I have my server running on my own machine on 8080 port. I have a realm called 'MyRealm'. Now when i run this application i get a below mentioned error.
P.S;-My basic aunthetication is only for /temp/* pages.

Sep 26, 2005 3:48:28 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: basic authentication scheme selected
Sep 26, 2005 3:48:28 PM org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
INFO: No credentials available for BASIC 'MyRealm'@localhost:8080

The program is as follows
public class BasicAuthHttpClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        
        // pass our credentials to HttpClient, they will only be used for
        // authenticating to servers with realm "realm" on the host
        // to authenticate against an arbitrary realm or host change the appropriate argument to null.
        client.getState().setCredentials(new AuthScope("http://localhost", 8080,"MyRealm"),new UsernamePasswordCredentials("MyRealm", "hello"));
        
        

        // create a GET method that reads a file over HTTP, we're assuming
        // that this file requires basic authentication using the realm above.
        GetMethod get = new GetMethod("http://localhost:8080/sample/temp/index.html");

        // Tell the GET method to automatically handle authentication. The
        // method will use any appropriate credentials to handle basic
        // authentication requests.  Setting this value to false will cause
        // any request for authentication to return with a status of 401.
        // It will then be up to the client to handle the authentication.
        get.setDoAuthentication( true );

        try {
            // execute the GET
            int status = client.executeMethod( get );

            // print the status and response
            System.out.println(status + "\n" + get.getResponseBodyAsString());

        } finally {
            // release any connection resources used by the method
            get.releaseConnection();
        }
    }
}

Why is it saying that no credentials are avaliable.
looking forward to urgent reply...
regards

Re: urgent: Problem using basic authentication

Posted by Roland Weber <RO...@de.ibm.com>.
Hi,

when creating the AuthScope, pass the host instead of a URL.
That's "localhost", not "http://localhost". My first idea was to
pass "'MyRealm'" instead of "MyRealm" :-)

hope that helps,
  Roland





"Ramit Bhardwaj" <ra...@cybage.com> 
26.09.2005 12:25
Please respond to
"HttpClient User Discussion"


To
<ht...@jakarta.apache.org>
cc

Subject
urgent: Problem using basic authentication






Hi guys,
I am using the sample application given on the commons httpclient site for 
basic authentication. I have my server running on my own machine on 8080 
port. I have a realm called 'MyRealm'. Now when i run this application i 
get a below mentioned error.
P.S;-My basic aunthetication is only for /temp/* pages.

Sep 26, 2005 3:48:28 PM 
org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: basic authentication scheme selected
Sep 26, 2005 3:48:28 PM org.apache.commons.httpclient.HttpMethodDirector 
processWWWAuthChallenge
INFO: No credentials available for BASIC 'MyRealm'@localhost:8080

The program is as follows
public class BasicAuthHttpClient {

                 /**
                  * @param args
                  */
                 public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
 
        // pass our credentials to HttpClient, they will only be used for
        // authenticating to servers with realm "realm" on the host
        // to authenticate against an arbitrary realm or host change the 
appropriate argument to null.
        client.getState().setCredentials(new AuthScope("http://localhost", 
8080,"MyRealm"),new UsernamePasswordCredentials("MyRealm", "hello"));
 
 

        // create a GET method that reads a file over HTTP, we're assuming
        // that this file requires basic authentication using the realm 
above.
        GetMethod get = new GetMethod("
http://localhost:8080/sample/temp/index.html");

        // Tell the GET method to automatically handle authentication. The
        // method will use any appropriate credentials to handle basic
        // authentication requests.  Setting this value to false will 
cause
        // any request for authentication to return with a status of 401.
        // It will then be up to the client to handle the authentication.
        get.setDoAuthentication( true );

        try {
            // execute the GET
            int status = client.executeMethod( get );

            // print the status and response
            System.out.println(status + "\n" + 
get.getResponseBodyAsString());

        } finally {
            // release any connection resources used by the method
            get.releaseConnection();
        }
    }
}

Why is it saying that no credentials are avaliable.
looking forward to urgent reply...
regards



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