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. ProgressDialog BackGround

Hello All,
  I would like to know the possibilities of changing the background
Image/color of aProgressDialog?

  I already tried it with a class that extends ProgressDialog and
tried to set the background, but it did not do it for the whole
Dialog(Window), only the part that has the Message get painted and not
the Bigger Container(Title+Message).

  Here is the Code Snippet

                                           mDefaultDialog = new
Dialog(mContext);
                
mDefaultDialog.setContentView(R.layout.progress_dialog_background);
                mDefaultDialog.setTitle("Custom Dialog");
                TextView text = (TextView) 
mDefaultDialog.findViewById(R.id.text);
                text.setText("Hello, this is a custom dialog!");

     Layout file

                            <LinearLayout
                                            xmlns:android="http://
schemas.android.com/apk/res/android"
 
android:layout_width="fill_parent"
 
android:layout_height="fill_parent"
 
android:orientation="horizontal"
 
android:background="@drawable/red">
                               <TextView android:id="@+id/text"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:text="Details"
                        android:textColor="#FFF"
                        android:textAppearance="@style/ScreenTitle" />
                           </LinearLayout>


Regards
V. Ramkumar.

-- 

2. About ListView filter

Hi every one,
    i want to know how to filter list view that have an custom
adapter.




Thanks

-- 

3. Use of AIDL Interfaces for IPC with List of AIDL Interface

4. Retrieve "My contact card" data

5. Dynamically obtaining several instance of a view item defined in layout/xml file

6. MapView displays pages different than launching via ACTION_VIEW

7. how to delete groups in android 2.1