Open browser with a url with extra headers for And

2019-04-24 13:43发布

I have a specific requirement where I have to fire a url on the browser from my activity. I am able to do this with the following code :

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

Now, I want to invoke this webseal_sso_endpoint by passing an extra header. say ("user":"username") How can I implement this? Thanks a lot in advance!

标签: java android uri
2条回答
\"骚年 ilove
2楼-- · 2019-04-24 14:25

I featured out how to add a header. Here is my code:

        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();
查看更多
叼着烟拽天下
3楼-- · 2019-04-24 14:29

Recommended method to do this would be to use the Uri class to create your URI. Helps ensure that everything is defined correctly and associates the correct key to value for your URI.

For example, you want to send a Web intent with this URL:

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

And you have a defined URL and parameters you want to send, you should declare these as static final fields, like so:

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

You can then use these, like so:

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

Now if you want to add another parameter, add another appendQueryParameter, like so:

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

You can convert to a URL if needed using:

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

Should come out like this:

http://webseal_sso_endpoint?authorization_code=SomeCode&webseal-ip=WEBSEALIP&user=SomeUsersName
查看更多
登录 后发表回答