Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open browser with a url with extra headers for Android

Tags:

java

android

uri

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!

like image 809
John Avatar asked Mar 05 '15 02:03

John


People also ask

How do you pass the browser header?

Fill out the Create a header fields as follows: In the Name field, enter the name of your header rule (for example, My header ). From the Type menu, select Request, and from the Action menu, select Set. In the Destination field, enter the name of the header affected by the selected action.

What is the code to open URL in Android?

A common way to achieve this is with the next code:String url = "http://www.stackoverflow.com"; Intent i = new Intent(Intent. ACTION_VIEW); i. setData(Uri. parse(url)); startActivity(i);


2 Answers

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();
like image 54
John Avatar answered Nov 07 '22 08:11

John


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
like image 25
fakataha Avatar answered Nov 07 '22 09:11

fakataha