Android Intent.ACTION_VIEW基本身份验证

时间:2015-01-04 19:27:45

标签: android android-intent android-fragments

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

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(),同样的交易。 Curl工作正常。

1 个答案:

答案 0 :(得分:5)

事实证明,您可以通过Bundle向Intent添加HTTP标头,并专门添加具有Base64编码用户ID的Authorization标头。

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