在Android手机Rails的服务器基本HTTP认证 [英] Basic HTTP Authentication on Android Phones to Rails Server

查看:264
本文介绍了在Android手机Rails的服务器基本HTTP认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到一个Rails应用服务器要求身份验证。我现在用的是雅加达HTTP客户端的Java在桌面应用程序和它的作品100%。但是,当完全相同的code是Android模拟器执行我得到一个IOException异常。

下面是code,如果有人可以帮助我弄清楚,为什么它会抛出IOException异常,这将是极大的AP preciated!

 私人布尔登录()
{
    字符串的用户名,密码;

    DefaultHttpClient客户端;
    AuthScope范围;
    凭据myCredentials;
    CredentialsProvider供应商;
    HttpEntity实体;
    串线;
    BufferedReader类阅读器;
    InputStream的河道内;

    //声明和放大器;创建HTTP客户端
    客户端=新DefaultHttpClient();

    //创建我们的AuthScope
    范围=新AuthScope(10.19.9.33,3000);

    用户名=管理
            密码=通行证


    //设置凭证
    myCredentials =新UsernamePasswordCredentials(用户名,密码);

    //设置提供商
    供应商=新BasicCredentialsProvider();
    provider.setCredentials(范围,myCredentials);

    //设置凭证
    client.setCredentialsProvider(供应商);

    字符串URL =htt​​p://10.19.9.33:3000/users/show/2;

    HTTPGET搞定;

    //告诉从哪里得到
    得到=新HTTPGET(URL);

    HTT presponse响应;

    尝试
    {
        响应= client.execute(获得);

        实体= response.getEntity();

        / *检查是否存在* /
        如果(实体!= NULL)
        {
            河道= entity.getContent();

            尝试 {

                读者=新的BufferedReader(新的InputStreamReader(河道));

                行= reader.readLine();

                如果(line.equals(HTTP基本:访问被拒绝。))
                    返回false;

                而(行!= NULL)
                {
                    //做一些事情的反应有用
                    的System.out.println(线);

                    行= reader.readLine();
                }

                返回true;

            }
            赶上(IOException异常前)
            {

                //如果一个IOException的连接将被释放
                //回连接管理器自动
                抛出前;

            }
            赶上(RuntimeException的前)
            {
                //如果发生意外的异常,你可能要中止
                //为了HTTP请求关闭底层
                //连接和释放回给连接管理器。
                get.abort();
                抛出前;
            }
            最后
            {
                //关闭输入流会触发连接释放
                instream.close();
            }
        }
    }
    赶上(ClientProtocolException cp_ex)
    {

    }
    赶上(IOException异常io_ex)
    {

    }

    返回false;
}
 

解决方案

它一直引发IOException异常是因为清单文件没有对互联网给申请权的理由

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
 

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;
}

解决方案

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>

这篇关于在Android手机Rails的服务器基本HTTP认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆