【问题标题】:Open browser with a url with extra headers for Android使用带有额外标头的 URL 打开浏览器,适用于 Android
【发布时间】:2015-03-05 02:03:36
【问题描述】:

我有一个特定要求,我必须从我的活动中触发浏览器上的 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。说 (“用户”:“用户名”) 我该如何实施? 提前非常感谢!

【问题讨论】:

    标签: java android uri


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    推荐的方法是使用 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
    

    【讨论】:

    • 只是在后面附加字符串不起作用。否则我可以在 Uri.parse(pref.getString("webseal_sso_endpoint", "") +"?authorization_code="+ code +"&webseal-ip="+ websealIP) 中附加更多属性。但是谢谢你的回答
    • 啊,也许我误解了你的目标。您是否尝试在应用程序和服务器之间创建会话?或者是其他东西?您能否编辑原始问题并提供示例或说明?
    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2021-06-06
    • 2017-05-31
    • 1970-01-01
    • 2020-05-27
    • 2014-01-09
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多