使用带有Android额外标题的url打开浏览器

我有一个特定的要求,我必须从我的活动中在浏览器上触发一个URL。 我可以使用以下代码执行此操作:

Intent browserIntent = new Intent( Intent.ACTION_VIEW, Uri.parse( pref.getString("webseal_sso_endpoint", "") + "?authorization_code=" + code + "&webseal-ip=" + websealIP ) ); activity.startActivity(browserIntent); activity.finish(); 

现在,我想通过传递一个额外的头来调用这个webseal_sso_endpoint。 说(“用户”:“用户名”)我该如何实现? 非常感谢提前!

建议的方法是使用Uri类创建URI。 帮助确保正确定义所有内容并将正确的键与URI的值相关联。

例如,您希望使用以下URL发送Web意图:

 http://webseal_sso_endpoint?authorization_code=SomeCode&webseal-ip=WEBSEALIP 

并且您有一个要发送的已定义的URL和参数,您应该将它们声明为静态最终字段,如下所示:

 private final static String BASE_URL = "http://webseal_sso_endpoint"; private final static String AUTH_CODE = "authorization_code"; private final static String IP = "webseal-ip"; private final static String USERNAME = "user"; 

然后你可以使用它们,如下所示:

 Uri builtUri = Uri.parse(BASE_URL).buildUpon() .appendQueryParameter(AUTH_CODE, code) .appendQueryParameter(IP, websealIP) .build(); 

现在,如果要添加另一个参数,请添加另一个appendQueryParameter,如下所示:

 Uri builtUri = Uri.parse(BASE_URL).buildUpon() .appendQueryParameter(AUTH_CODE, code) .appendQueryParameter(IP, websealIP) .appendQueryParameter(USERNAME, user) .build(); 

您可以根据需要使用以下内容转换为URL:

 URL url = new URL(builtUri.toString()); 

应该这样出来:

 http://webseal_sso_endpoint?authorization_code=SomeCode&webseal-ip=WEBSEALIP&user=SomeUsersName 

我详细介绍了如何添加标题。 这是我的代码:

  Intent browserIntent = new Intent( Intent.ACTION_VIEW, Uri.parse(url)); Bundle bundle = new Bundle(); bundle.putString("iv-user", username); browserIntent.putExtra(Browser.EXTRA_HEADERS, bundle); activity.startActivity(browserIntent); activity.finish();