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