Android Intent.ACTION_VIEW基本身份验证 [英] Android Intent.ACTION_VIEW Basic Authentication

查看:662
本文介绍了Android Intent.ACTION_VIEW基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将HTTP基本身份验证信息传递给Intent.ACTION_VIEW?这是我表达意图的地方:

How do I pass along HTTP Basic Authentication information to Intent.ACTION_VIEW? Here's where I'm firing the intent:

public class OutageListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    // ...

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);

        // Get a URI for the selected item, then start an Activity that displays the URI. Any
        // Activity that filters for ACTION_VIEW and a URI can accept this. In most cases, this will
        // be a browser.
        String outageUrlString = "http://demo:demo@demo.opennms.org/opennms/outage/detail.htm?id=204042";
        Log.i(TAG, "Opening URL: " + outageUrlString);
        // Get a Uri object for the URL string
        Uri outageURI = Uri.parse(outageUrlString);
        Intent i = new Intent(Intent.ACTION_VIEW, outageURI);
        startActivity(i)
    }

}

我也尝试过Uri.fromParts(),同样的方法.卷毛效果很好.

I have also tried Uri.fromParts(), same deal. Curl works just fine.

推荐答案

原来,您可以通过捆绑包将HTTP标头添加到Intent,并特别添加具有Base64编码用户ID的Authorization标头.

Turns out you can add HTTP headers to the Intent via a Bundle, and specifically add an Authorization header with a Base64 encoded user id.

    Intent i = new Intent(Intent.ACTION_VIEW, outageURI);

    String authorization = user + ":" + password;
    String authorizationBase64 = Base64.encodeToString(authorization.getBytes(), 0);

    Bundle bundle = new Bundle();
    bundle.putString("Authorization", "Basic " + authorizationBase64);
    i.putExtra(Browser.EXTRA_HEADERS, bundle);
    Log.d(TAG, "intent:" + i.toString());

    startActivity(i);

这篇关于Android Intent.ACTION_VIEW基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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