Android Basic Authentication Intent.ACTION_VIEW

How do I pass basic HTTP authentication information to Intent.ACTION_VIEW

? Here's where I dismiss 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)
    }

}

      

I have also tried Uri.fromParts()

, the same deal. Curl works great.

+3


source to share


1 answer


It turns out that you can add HTTP headers to the Intent via the 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);

      

+4


source







All Articles