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. Out of Memory Exception when exporting vcards

Hi

I am facing a problem when I need to export vcards overs bluetooth.
Problem is vcards are large in number. Plus If the vcards have images in
them then size is really high.

So when i try sending this I get OutOfMemory Exception , its Coming from
VcardComposer.java. - At onEntryCreated()

Can I make more memory allocation on the go? Is this possible.
Dalvik basically cannot allocate memory to these data.

Please advice.

Thanks & Regards,
Ajith

PS: Log is below :

-17 11:42:31.669 D/dalvikvm(  740): GC_CONCURRENT freed 1943K, 52% free
12605K/26183K, external 3450K/4213K, paused 2ms+4ms
03-17 11:42:31.970 D/dalvikvm(  740): GC_CONCURRENT freed 1954K, 52% free
12576K/26183K, external 3450K/4213K, paused 2ms+4ms
03-17 11:42:32.369 D/dalvikvm(  740): GC_CONCURRENT freed 1980K, 52% free
12642K/26183K, external 3450K/4213K, paused 2ms+3ms
03-17 11:42:32.556 D/dalvikvm(  740): GC_FOR_MALLOC freed 1412K, 53% free
12541K/26183K, external 3450K/4213K, paused 22ms
03-17 11:42:32.556 I/dalvikvm-heap(  740): Forcing collection of
SoftReferences for 16760848-byte allocation
03-17 11:42:32.576 D/dalvikvm(  740): GC_FOR_MALLOC freed <1K, 53% free
12540K/26183K, external 3450K/4213K, paused 19ms
03-17 11:42:32.576 E/dalvikvm-heap(  740): Out of memory on a 16760848-byte
allocation.
03-17 11:42:32.576 I/dalvikvm(  740): "Binder Thread #2" prio=5 tid=8
RUNNABLE
03-17 11:42:32.576 I/dalvikvm(  740):   | group="main" sCount=0 dsCount=0
obj=0x40520920 self=0xea818
03-17 11:42:32.576 I/dalvikvm(  740):   | sysTid=747 nice=10 sched=0/0
cgrp=bg_non_interactive handle=583480
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~91)
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.OutputStreamWriter.flush(OutputStreamWriter.java:164)
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.OutputStreamWriter.convert(OutputStreamWriter.java:236)
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.OutputStreamWriter.write(OutputStreamWriter.java:225)
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.BufferedWriter.write(BufferedWriter.java:301)
03-17 11:42:32.576 I/dalvikvm(  740):   at
java.io.Writer.write(Writer.java:152)
03-17 11:42:32.576 I/dalvikvm(  740):   at
android.pim.vcard.VCardComposer$HandlerForOutputStream.onEntryCreated(VCardComposer.java:212)
03-17 11:42:32.576 I/dalvikvm(  740):   at
android.pim.vcard.VCardComposer.createOneEntry(VCardComposer.java:512)
03-17 11:42:32.576 I/dalvikvm(  740):   at
android.pim.vcard.VCardComposer.createOneEntry(VCardComposer.java:466)
03-17 11:42:32.576 I/dalvikvm(  740):   at
com.android.providers.contacts.ContactsProvider2.outputRawContactsAsVCard(ContactsProvider2.java:5594)
03-17 11:42:32.580 I/dalvikvm(  740):   at
com.android.providers.contacts.ContactsProvider2.openAssetFile(ContactsProvider2.java:5524)
03-17 11:42:32.580 I/dalvikvm(  740):   at
android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:242)
03-17 11:42:32.580 I/dalvikvm(  740):   at
android.content.ContentProviderNative.onTransact(ContentProviderNative.java:234)
03-17 11:42:32.580 I/dalvikvm(  740):   at
android.os.Binder.execTransact(Binder.java:320)
03-17 11:42:32.580 I/dalvikvm(  740):   at
dalvik.system.NativeStart.run(Native Method)
03-17 11:42:32.580 I/dalvikvm(  740):
03-17 11:42:32.580 E/JavaBinder(  740): *** Uncaught remote exception!
(Exceptions are not yet supported across processes.)
03-17 11:42:32.580 E/JavaBinder(  740): java.lang.OutOfMemoryError
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.OutputStreamWriter.flush(OutputStreamWriter.java:164)
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.OutputStreamWriter.convert(OutputStreamWriter.java:236)
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.OutputStreamWriter.write(OutputStreamWriter.java:225)
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.BufferedWriter.write(BufferedWriter.java:301)
03-17 11:42:32.580 E/JavaBinder(  740):  at
java.io.Writer.write(Writer.java:152)
03-17 11:42:32.580 E/JavaBinder(  740):  at
android.pim.vcard.VCardComposer$HandlerForOutputStream.onEntryCreated(VCardComposer.java:212)
03-17 11:42:32.580 E/JavaBinder(  740):  at
android.pim.vcard.VCardComposer.createOneEntry(VCardComposer.java:512)
03-17 11:42:32.580 E/JavaBinder(  740):  at
android.pim.vcard.VCardComposer.createOneEntry(VCardComposer.java:466)
03-17 11:42:32.580 E/JavaBinder(  740):  at
com.android.providers.contacts.ContactsProvider2.outputRawContactsAsVCard(ContactsProvider2.java:5594)
03-17 11:42:32.580 E/JavaBinder(  740):  at
com.android.providers.contacts.ContactsProvider2.openAssetFile(ContactsProvider2.java:5524)
03-17 11:42:32.580 E/JavaBinder(  740):  at
android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:242)
03-17 11:42:32.580 E/JavaBinder(  740):  at
android.content.ContentProviderNative.onTransact(ContentProviderNative.java:234)
03-17 11:42:32.580 E/JavaBinder(  740):  at
android.os.Binder.execTransact(Binder.java:320)
03-17 11:42:32.580 E/JavaBinder(  740):  at
dalvik.system.NativeStart.run(Native Method)
03-17 11:42:32.580 W/dalvikvm(  740): threadid=8: thread exiting with
uncaught exception (group=0x40015560)
03-17 11:42:32.580 E/AndroidRuntime(  740): FATAL EXCEPTION: Binder Thread
#2
03-17 11:42:32.580 E/AndroidRuntime(  740): java.lang.OutOfMemoryError
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.OutputStreamWriter.flush(OutputStreamWriter.java:164)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.OutputStreamWriter.convert(OutputStreamWriter.java:236)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.OutputStreamWriter.write(OutputStreamWriter.java:225)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.BufferedWriter.write(BufferedWriter.java:301)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
java.io.Writer.write(Writer.java:152)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
android.pim.vcard.VCardComposer$HandlerForOutputStream.onEntryCreated(VCardComposer.java:212)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
android.pim.vcard.VCardComposer.createOneEntry(VCardComposer.java:512)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
android.pim.vcard.VCardComposer.createOneEntry(VCardComposer.java:466)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
com.android.providers.contacts.ContactsProvider2.outputRawContactsAsVCard(ContactsProvider2.java:5594)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
com.android.providers.contacts.ContactsProvider2.openAssetFile(ContactsProvider2.java:5524)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:242)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
android.content.ContentProviderNative.onTransact(ContentProviderNative.java:234)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
android.os.Binder.execTransact(Binder.java:320)
03-17 11:42:32.580 E/AndroidRuntime(  740):  at
dalvik.system.NativeStart.run(Native Method)
03-17 11:42:32.587 W/ActivityManager(  116):   Force finishing activity
com.android.contacts/.DialtactsActivity
03-17 11:42:32.634 V/RenderScript_jni(  724): surfaceCreated
03-17 11:42:32.634 V/RenderScript_jni(  724): surfaceChanged

-- 

2. Camera Object is not passed during Video recording in Android.

Hello,

We are trying to implement Real time Video Based Steganography in
Android based Smart phone.

I am using Sony Xperia X8 With Android 2.1.

My actual problem is, when I try to fetch the frames from video on
camera object it will not fetched and application is crashed.

Give me you Email Id so I can send my code.

Hope for helpful reply.

Your's faithfully
Nilay R. Mistry

-- 

3. Stack Traces only referencing framework code

4. Quick Search Box Limit

5. json data to array list

6. AppWidget Stopped !!!

7. Dynamically including a custom view defined in another app