Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic HTTP Authentication on Android Phones to Rails Server

I'm trying to connect to a Rails Application Server that requires authentication. I am using the Jakarta HTTP Client for Java on a Desktop application and it works 100%. But when the exact same code is executed on the Android Emulator I get an IOException.

Here is the code, and if anyone could help me figure out why it throws the IOException that would be greatly appreciated!

private boolean login()
{
    String username, password;

    DefaultHttpClient client;
    AuthScope scope;
    Credentials myCredentials;
    CredentialsProvider provider;
    HttpEntity entity;
    String line;
    BufferedReader reader;
    InputStream instream;

    //Declare & Create the HTTP Client  
    client = new DefaultHttpClient();

    //Create our AuthScope
    scope = new AuthScope("10.19.9.33", 3000);

    username = "admin"
            password = "pass"


    //Set Credentials
    myCredentials = new UsernamePasswordCredentials( username, password );

    //Set Provider
    provider = new BasicCredentialsProvider();
    provider.setCredentials(scope, myCredentials);

    //Set Credentials
    client.setCredentialsProvider( provider );

    String url = "http://10.19.9.33:3000/users/show/2";

    HttpGet get;

    //Tell where to get
    get = new HttpGet( url );

    HttpResponse response;

    try
    {
        response = client.execute( get );

        entity = response.getEntity();

        /* Check to see if it exists */
        if( entity != null )
        {
            instream = entity.getContent();

            try {

                reader = new BufferedReader(new InputStreamReader(instream));

                line = reader.readLine();

                if( line.equals( "HTTP Basic: Access denied.") )
                    return false;

                while ( line != null )
                {
                    // do something useful with the response
                    System.out.println(line);

                    line = reader.readLine();
                }

                return true;

            } 
            catch (IOException ex)
            {

                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;

            } 
            catch (RuntimeException ex)
            {
                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying 
                // connection and release it back to the connection manager.
                get.abort();
                throw ex;               
            } 
            finally
            {
                // Closing the input stream will trigger connection release
                instream.close();               
            }
        }
    }
    catch( ClientProtocolException cp_ex )
    {

    }
    catch( IOException io_ex )
    {

    }

    return false;
}
like image 688
Christopher MacKinnon Avatar asked Nov 24 '10 17:11

Christopher MacKinnon


1 Answers

The reason it kept triggering the IOException was because the Manifest file didn't give the Application rights to the internet

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
like image 74
Christopher MacKinnon Avatar answered Oct 03 '22 05:10

Christopher MacKinnon