java URL追加query parameters

来源:互联网 发布:php判断是否为空 编辑:程序博客网 时间:2024/06/11 05:30

用法:String aimUrl = appendUri(showDetailUrl, "token=" + token);

public class URITest {    public static String appendUri(String uri, String appendQuery) throws URISyntaxException {        URI oldUri = new URI(uri);        String newQuery = oldUri.getQuery();        if (newQuery == null) {            newQuery = appendQuery;        } else {            newQuery += "&" + appendQuery;        }        URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(),                oldUri.getPath(), newQuery, oldUri.getFragment());        return newUri.toString();    }    public static void main(String[] args) throws Exception {        System.out.println(appendUri("http://example.com", "name=John"));        System.out.println(appendUri("http://example.com#fragment", "name=John"));        System.out.println(appendUri("http://example.com?email=john.doe@email.com", "name=John"));        System.out.println(appendUri("http://example.com?email=john.doe@email.com#fragment", "name=John"));    }}
阅读全文
1 0
原创粉丝点击