Android 手机上对 Rails Server 的基本 HTTP 身份验证

发布于 2024-10-04 02:01:28 字数 2782 浏览 6 评论 0原文

我正在尝试连接到需要身份验证的 Rails 应用程序服务器。我在桌面应用程序上使用 Jakarta HTTP Client for Java,它运行 100%。但是,当在 Android 模拟器上执行完全相同的代码时,我收到 IOException。

这是代码,如果有人能帮助我弄清楚为什么它会抛出 IOException,我将不胜感激!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-10-11 02:01:28

它不断触发 IOException 的原因是 Manifest 文件没有授予应用程序访问互联网的权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

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>
泅渡 2024-10-11 02:01:28

我正在使用 HttpPost 来完成此类任务,并且从未遇到过任何问题:

[...]
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(LOGIN_SERVLET_URI);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();
[...]

也许这可以帮助您

I'm using HttpPost for this kind of task, and never had any problem:

[...]
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(LOGIN_SERVLET_URI);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();
[...]

maybe this helps you out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文