Open browser with a url with extra headers for Android

后端 未结 2 977
难免孤独
难免孤独 2021-02-20 15:11

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 = n         


        
2条回答
  •  孤独总比滥情好
    2021-02-20 15:48

    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
    

提交回复
热议问题