Basic HTTP authentication

by Wouter » Wed, 29 Apr 2009 06:26:42 GMT


Sponsored Links
 Hey,

I have created a web application in ruby on rails and I want to use it
now in my android application.
I use http basic authentication in my website but I don't get any
success to authenticate me trough the android application..

I have protected resources and pages on the website that needs
authentication to access it! How can I get this right in my
application?

How can i do the authentication? i have tried with a authscope and
BasicCredentialsProvider but i think i am not doing it good!

Can someone help me!

Thanx,

Wouter
--~--~---------~--~----~------------~-------~--~----~



Basic HTTP authentication

by Jon Colverson » Wed, 29 Apr 2009 07:01:58 GMT


 


I think this is all you need:

    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(
        new AuthScope(HOST, PORT),
        new UsernamePasswordCredentials(username, password));

--
Jon

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Basic HTTP authentication

by Wouter » Wed, 29 Apr 2009 16:43:39 GMT


 No this doesn't work..

I have tried to access my page with curl (curl  http://192.168.1.66 :3000/me
--basic -u wouter:test) and I got a successfull xml response!

So how can I do this in Android? The code you give didn't work..





--~--~---------~--~----~------------~-------~--~----~



Basic HTTP authentication

by Wouter » Thu, 30 Apr 2009 01:06:22 GMT


 I cannot access my page! How can I do this?





--~--~---------~--~----~------------~-------~--~----~



Basic HTTP authentication

by Jon Colverson » Thu, 30 Apr 2009 12:25:21 GMT


 


Can you post your code?

--
Jon

--~--~---------~--~----~------------~-------~--~----~



Basic HTTP authentication

by Wouter » Thu, 30 Apr 2009 16:14:01 GMT


  have tried to do it with the normal HttpClient and now I have done
it with the apache commons HttpClient:


On Apr 30, 6:25am, Jon Colverson <jjc1...@gmail.com> wrote:

public class httpTest extends Activity{



private HttpClient httpClient = new HttpClient();

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
retreiveProjects();
}

private void retreiveProjects()
{


httpClient.getParams().setBooleanParameter
( HttpClientParams.REJECT_RELATIVE_REDIRECT, false);

httpClient.getParams().setBooleanParameter
( HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); //true

httpClient.getHostConfiguration().getParams().setParameter
("http..version", HttpVersion.HTTP_1_1);

httpClient.getHostConfiguration().setHost("192.168.1.66",
3000, "http");

HttpState initialState = new HttpState();
initialState.setCredentials(new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT), new UsernamePasswordCredentials("wouter",
"sleutel"));

httpClient.setState(initialState);


httpClient.getState().setCredentials(
new AuthScope(AuthScope.ANY_HOST, 3000, "realm"),
new UsernamePasswordCredentials("wouter", "sleutel")
);

httpClient.getParams().setCookiePolicy
(CookiePolicy.RFC_2109);
GetMethod getMethodInfoPage = new GetMethod("http://
192.168.1.66:3000/users.xml");
getMethodInfoPage.setDoAuthentication(true);
getMethodInfoPage.addRequestHeader("Accept", "image/gif,
image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-
flash, application/vnd.ms-excel, application/vnd.ms-powerpoint,
application/msword, */*");

getMethodInfoPage.addRequestHeader("Accept-Encoding", "gzip,
deflate");

getMethodInfoPage.addRequestHeader("Accept-Language", "en-
us");

System.out.println("\n" + "About to make call for
getMethodInfoPage connection attempt");

int status;
try {
httpClient.executeMethod(getMethodInfoPage);

}
catch (IOException ex2) {
ex2.printStackTrace();
}




/* // create a thread-safe connection manager. Allows multiple
threaded requests from client
ThreadSafeClientConnManager multiThreadedConnectionManager =
new ThreadSafeClientConnManager(httpParams, registry);
httpClient = new DefaultHttpClient
(multiThreadedConnectionManager, httpParams);

// we only support basic authentication
BasicScheme basicScheme = new BasicScheme();
AuthSchemeRegistry authRegistry = new AuthSchemeRegistry();
authRegistry.register(basicScheme.getSchemeName(), new
BasicSchemeFactory());
this.httpClient.setAuthSchemes(authRegistry);
this.httpClient.setCredentialsProvider(new
BasicCredentialsProvider());



Basic HTTP authentication

by Wouter » Thu, 30 Apr 2009 16:16:21 GMT


 




I have tried to do it with the normal HttpClient and now I have done
it with the apache commons HttpClient:

public class httpTest extends Activity{



         private HttpClient httpClient = new HttpClient();

          @Override
          public void onCreate(Bundle savedInstanceState)
          {
            super.onCreate(savedInstanceState);
            retreiveProjects();
          }

          private void retreiveProjects()
          {


                  httpClient.getParams().setBooleanParameter
( HttpClientParams.REJECT_RELATIVE_REDIRECT, false);

                httpClient.getParams().setBooleanParameter
( HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); //true

                httpClient.getHostConfiguration().getParams().setParameter
("http..version", HttpVersion.HTTP_1_1);

                httpClient.getHostConfiguration().setHost("192.168.1.66",
3000, "http");

                HttpState initialState = new HttpState();
                initialState.setCredentials(new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT), new UsernamePasswordCredentials("wouter",
"sleutel"));

                httpClient.setState(initialState);


                httpClient.getState().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, 3000, "realm"),
                        new UsernamePasswordCredentials("wouter", "sleutel")
                    );

                httpClient.getParams().setCookiePolicy
(CookiePolicy.RFC_2109);
                GetMethod getMethodInfoPage = new GetMethod("http://
192.168.1.66:3000/users.xml");
                getMethodInfoPage.setDoAuthentication(true);
                 getMethodInfoPage.addRequestHeader("Accept", "image/gif,
image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-
flash, application/vnd.ms-excel, application/vnd.ms-powerpoint,
application/msword, */*");

                 getMethodInfoPage.addRequestHeader("Accept-Encoding", "gzip,
deflate");

                 getMethodInfoPage.addRequestHeader("Accept-Language", "en-
us");

                 System.out.println("\n" + "About to make call for
getMethodInfoPage connection attempt");

                 int status;
                 try {
                   httpClient.executeMethod(getMethodInfoPage);

                 }
                  catch (IOException ex2) {
                          ex2.printStackTrace();
                  }






          }

I am trying to get a restful resource on my localhost! Do i have to
use the ip from my localhost or am i doing something wrong?
But whren it seems that it don't pass my username and password when I
try to access it!
Like i said i tried it with curl when passing my username and password
and i got a successfull response!

Hope you can help me!

--~--~---------~--~----~------------~-------~--~----~



Basic HTTP authentication

by Mark Murphy » Thu, 30 Apr 2009 17:36:34 GMT


 outer wrote:

When seeking assistance on Apache HttpClient, you are probably better
off asking the Apache HttpClient project.

That being said, what you appear to be attempting to do is "preemptive
HTTP authentication" (providing credentials initially, rather than
responding to a 401 Unauthorized response). HttpClient appears to be bad
at this.

If you visit:

http://commonsware.com/AndTutorials

and download the source code (link on that page), look at tutorial
17-HttpClient. There, I have logic using HttpClient that performs
preemptive HTTP authentication -- hacking in the HTTP authentication
header using Base64 encoding.

You may find URLConnection to be a better means of doing preemptive HTTP
authentication. For example, that is what the JTwitter JAR uses under
the covers, since Twitter requires preemptive HTTP authentication.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Scrollable area like in Robo Defense game(both horizontal and vertical)

I would imagine that robo defence is doing custom scrolling within their
application, and not relying on a UI component for that piece.

- Dan





--~--~---------~--~----~------------~-------~--~----~

2. How to change /sdcard to be not a read-only file system

Hi,

I am trying to copy an image to the emulator sd card by:

$ adb push ~/Desktop/bullet_ball_glass_red.png
/sdcard/bullet_ball_glass_red.png

But I get an error saying:
failed to copy '/Users/n179911/Desktop/bullet_ball_glass_red.png' to
'/sdcard/bullet_ball_glass_red.png': Read-only file system

So I tried to go to adb shell to manual change the /sdcard to non
read-only, but it still wont let me.
$ adb shell
# su
# chmod 777 /sdcard
Unable to chmod /sdcard: Read-only file system
#

--~--~---------~--~----~------------~-------~--~----~

3. How to use apache commons in my android application

4. How to change /sdcard to be not a read-only file system

5. laying out buttons with transparent backgrounds on a linear layout with a non-transparent background...

6. Fwd: Hero Rom

7. How do I submit an App in a different language?