【问题标题】:How to do HTTP authentication in android?如何在android中进行HTTP身份验证?
【发布时间】:2010-12-30 10:02:09
【问题描述】:

我正在检查类 org.apache.http.auth。 如果有人有更多的参考或例子吗?

【问题讨论】:

  • 这是一个关于 Android 应用程序身份验证的问题,还是只是关于可能在 Android 上运行的一般网络应用程序的身份验证?
  • 用于用户凭据(用户名、密码)的 Web 身份验证(http 身份验证)

标签: android http-authentication


【解决方案1】:

我之前没有遇到过那个特定的包,但它说它是用于客户端 HTTP 身份验证的,我已经能够在 Android 上使用 java.net API 执行此操作,如下所示:

Authenticator.setDefault(new Authenticator(){
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("myuser","mypass".toCharArray());
    }});
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
c.setUseCaches(false);
c.connect();

显然你的 getPasswordAuthentication() 应该做一些比返回常量更智能的事情。

如果您尝试使用带有身份验证的正文(例如POST)发出请求,请注意Android issue 4326。我已经将建议的修复链接到那里的平台,但是如果您只想要基本身份验证,有一个简单的解决方法:不要打扰 Authenticator,而是这样做:

c.setRequestProperty("Authorization", "basic " +
        Base64.encode("myuser:mypass".getBytes(), Base64.NO_WRAP));

【讨论】:

  • 你知道android 2.0中有什么base64编码类吗??
  • 该平台在一些地方有它,但奇怪的是他们没有暴露它。他们甚至在例如的文档中留下了对它的引用。 android.util。当我发现这个时,我正在使用ksoap2-android,他们有一个只依赖于java.io 的实现,所以你可以从:kobjects.cvs.sourceforge.net/kobjects/kobjects/src/org/kobjects/…
  • 您如何处理身份验证失败的事件,比如提供的凭据错误?
  • Base64 在旧版 Android 中不可用。有什么建议吗?
  • 我在尝试 c.connect() 时遇到错误; ,它说 IOException
【解决方案2】:

对我来说,它有效,

final String basicAuth = "Basic " + Base64.encodeToString("user:password".getBytes(), Base64.NO_WRAP);

Apache HttpCLient:

request.setHeader("Authorization", basicAuth);

HttpUrlConnection:

connection.setRequestProperty ("Authorization", basicAuth);

【讨论】:

  • NO_WRAP 标志!那是关键。我只是使用默认值,想知道为什么我一直得到 400。
  • 您的回答为我节省了大量时间。谢谢!我的问题确实是错误的标志(默认)。
  • 终于,过了好久... no_wrap FTW!
  • 谢谢哥们,这是核心:)
  • 感谢您的简洁回答! Android 社​​区 FTW。
【解决方案3】:

这对我有用

 URL imageUrl = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) imageUrl
                            .openConnection();
                    conn.setRequestProperty("Authorization", "basic " +
                            Base64.encode("username:password".getBytes()));
                    conn.setConnectTimeout(30000);
                    conn.setReadTimeout(30000);
                    conn.setInstanceFollowRedirects(true);
                    InputStream is = conn.getInputStream();

【讨论】:

    【解决方案4】:

    手动方法适用于 import android.util.Base64,但请务必在调用编码时设置 Base64.NO_WRAP:

    String basicAuth = "Basic " + new String(Base64.encode("user:pass".getBytes(),Base64.NO_WRAP ));
    connection.setRequestProperty ("Authorization", basicAuth);
    

    【讨论】:

      【解决方案5】:

      您可以手动插入http头请求:

      HttpGet request = new HttpGet(...);
      request.setHeader("Authorization", "Basic "+Base64.encodeBytes("login:password".getBytes()));
      

      【讨论】:

        【解决方案6】:

        对于我的 Android 项目,我从这里使用了 Base64 库:

        http://iharder.net/base64

        这是一个非常广泛的图书馆,到目前为止我没有遇到任何问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-10
          • 1970-01-01
          • 2016-05-11
          • 1970-01-01
          相关资源
          最近更新 更多