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 alee amin <ma...@gmail.com> on 2009/02/04 14:51:23 UTC

unable to do basic authentication

hi,
I am trying to send a request to my own website which is using basic
authentication method. But i am unable to to authenticate it through
httpclient

here is the code i am using

        HttpClient httpClient = new HttpClient();
        HttpMethod httpGet = new GetMethod("
http://localhost:8080/project-web/Servlet");

        httpClient.getState().setCredentials(
                new AuthScope("http://localhost:8080", 443, "realm"),
                new UsernamePasswordCredentials("userName", "Password"));

        httpGet.setDoAuthentication(true);

        try{
            int status = httpClient.executeMethod(httpGet);
            byte[] response = httpGet.getResponseBody();
            System.out.println("status: " + status);
            System.out.println("response: " + new String(response));
        }
        catch (Exception ex){
        }

There is no exception that is being raised. I am getting the status code 401
with "responseBody" having text ... "this request require authentication".
Am i missing something?
..alee
http://techboard.wordpress.com

Re: unable to do basic authentication

Posted by alee amin <ma...@gmail.com>.
ok - i got it. i was authenticating it on 443 - now i have changed to the
appropriate port. it worked. thanks :)
..alee
http://techboard.wordpress.com



On Wed, Feb 4, 2009 at 6:51 PM, alee amin <ma...@gmail.com>wrote:

> hi,
> I am trying to send a request to my own website which is using basic
> authentication method. But i am unable to to authenticate it through
> httpclient
>
> here is the code i am using
>
>         HttpClient httpClient = new HttpClient();
>         HttpMethod httpGet = new GetMethod("
> http://localhost:8080/project-web/Servlet");
>
>         httpClient.getState().setCredentials(
>                 new AuthScope("http://localhost:8080", 443, "realm"),
>                 new UsernamePasswordCredentials("userName", "Password"));
>
>         httpGet.setDoAuthentication(true);
>
>         try{
>             int status = httpClient.executeMethod(httpGet);
>             byte[] response = httpGet.getResponseBody();
>             System.out.println("status: " + status);
>             System.out.println("response: " + new String(response));
>         }
>         catch (Exception ex){
>         }
>
> There is no exception that is being raised. I am getting the status code
> 401 with "responseBody" having text ... "this request require
> authentication". Am i missing something?
> ..alee
> http://techboard.wordpress.com
>
>