JAVA URIBuilder构建URL

String url = "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=";
    URIBuilder uriBuilder = new URIBuilder(url);
    System.out.println(uriBuilder.getScheme());
    System.out.println(uriBuilder.getUserInfo());
    System.out.println(uriBuilder.getHost());
    System.out.println(uriBuilder.getPort());
    System.out.println(uriBuilder.getPath());
    System.out.println(uriBuilder.getQueryParams());
    System.out.println(uriBuilder.getFragment());
    System.out.println(uriBuilder.getCharset());
输出如下所示:
    scheme:http
    userinfo:null
    host:www.google.com
    prot:-1  端口默认是80,当显示指定端口时,此处便能取到值
    path:/search
    queryParams:[hl=en, q=httpclient, btnG=Google Search, aq=f, oq=]
    fragment:null
    charset:null
    某些属性含义未知手动设置一次
    String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
    URIBuilder uriBuilder = new URIBuilder(url);
    uriBuilder.setFragment("111");
    uriBuilder.setUserInfo("222", "333");
    uriBuilder.setCharset(new GBK());
    System.out.println(uriBuilder.build());
    System.out.println(uriBuilder.getScheme());
    System.out.println(uriBuilder.getUserInfo());
    System.out.println(uriBuilder.getHost());
    System.out.println(uriBuilder.getPath());
    System.out.println(uriBuilder.getQueryParams());
    System.out.println(uriBuilder.getFragment());
    System.out.println(uriBuilder.getCharset());
    输出如下:
    http://222:[email protected]/football/info/fb_match_hhad.php?m=102909#111
    scheme:http
    userInfo:222:333   没见过这东西-。-
    host:info.sporttery.cn
    path:/football/info/fb_match_hhad.php
    queryParams:[m=102909]
    fragment:111   路由
    charset:GBK

URIBuilder addParameter(final String param, final String value) 

String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
    URIBuilder uriBuilder = new URIBuilder(url);
    uriBuilder.setParameter("m", "2");
    uriBuilder.addParameter("m" ,"5");
    System.out.println(uriBuilder.build());
    输出:http://info.sporttery.cn/football/info/fb_match_hhad.php?m=2&m=5
    得出结论:set会覆盖原来的同名参数而add不会

 URIBuilder clearParameters()

String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
    URIBuilder uriBuilder = new URIBuilder(url);
    uriBuilder.clearParameters();
    System.out.println(uriBuilder.build());
    输出:http://info.sporttery.cn/football/info/fb_match_hhad.php
    就是清除所有参数

 

你可能感兴趣的:(java)