依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.6</version> </dependency>
url查询参数构建
package com.example; import cn.hutool.core.net.url.UrlQuery; import org.junit.jupiter.api.Test; public class UrlTest { @Test public void testUrlBuild(){ UrlQuery urlQuery = new UrlQuery(); urlQuery.add("name", "Tom"); urlQuery.add("age", 23); System.out.println(urlQuery.toString()); // name=Tom&age=23 } }
完整url构建
package com.example; import cn.hutool.core.net.url.UrlBuilder; import org.junit.jupiter.api.Test; public class UrlTest { @Test public void testUrlBuild(){ String buildUrl = UrlBuilder.create() .setScheme("https") .setHost("www.hutool.cn") .addPath("/s") .addQuery("ie", "UTF-8") .addQuery("ie", "GBK") .addQuery("wd", "测试") .build(); System.out.println(buildUrl); // https://www.hutool.cn/s?ie=UTF-8&ie=GBK&wd=%E6%B5%8B%E8%AF%95 } }