Search in sources :

Example 41 with UrlBuilder

use of cn.hutool.core.net.url.UrlBuilder in project hutool by dromara.

the class UrlBuilderTest method gimg2Test.

@Test
public void gimg2Test() {
    String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F1114%2F0H320120Z3%2F200H3120Z3-6-1200.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621996490&t=8c384c2823ea453da15a1b9cd5183eea";
    final UrlBuilder urlBuilder = UrlBuilder.of(url);
    // PATH除了第一个path外,:是允许的
    String url2 = "https://gimg2.baidu.com/image_search/src=http:%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F1114%2F0H320120Z3%2F200H3120Z3-6-1200.jpg&refer=http:%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621996490&t=8c384c2823ea453da15a1b9cd5183eea";
    Assert.assertEquals(url2, urlBuilder.toString());
}
Also used : UrlBuilder(cn.hutool.core.net.url.UrlBuilder) Test(org.junit.Test)

Example 42 with UrlBuilder

use of cn.hutool.core.net.url.UrlBuilder in project hutool by dromara.

the class UrlBuilderTest method testEncodeInQuery.

@Test
public void testEncodeInQuery() {
    // b=4?6  参数中有未编码的?
    String webUrl = "http://exmple.com/patha/pathb?a=123&b=4?6&c=789";
    final UrlBuilder urlBuilder = UrlBuilder.of(webUrl, StandardCharsets.UTF_8);
    Assert.assertEquals("a=123&b=4?6&c=789", urlBuilder.getQueryStr());
}
Also used : UrlBuilder(cn.hutool.core.net.url.UrlBuilder) Test(org.junit.Test)

Example 43 with UrlBuilder

use of cn.hutool.core.net.url.UrlBuilder in project hutool by dromara.

the class UrlQueryTest method ofHttpWithoutEncodeTest.

@Test
public void ofHttpWithoutEncodeTest() {
    // charset为null表示不做编码
    String url = "https://img-cloud.voc.com.cn/140/2020/09/03/c3d41b93e0d32138574af8e8b50928b376ca5ba61599127028157.png?imageMogr2/auto-orient/thumbnail/500&pid=259848";
    final UrlBuilder urlBuilder = UrlBuilder.ofHttpWithoutEncode(url);
    final String queryStr = urlBuilder.getQueryStr();
    Assert.assertEquals("imageMogr2/auto-orient/thumbnail/500&pid=259848", queryStr);
}
Also used : UrlBuilder(cn.hutool.core.net.url.UrlBuilder) Test(org.junit.Test)

Example 44 with UrlBuilder

use of cn.hutool.core.net.url.UrlBuilder in project hutool by dromara.

the class UrlBuilderTest method testChineseFragmentWithPath.

@Test
public void testChineseFragmentWithPath() {
    String buildUrl = new UrlBuilder().setScheme("https").setHost("www.hutool.cn").addPath("/s").setFragment("测试").build();
    Assert.assertEquals("https://www.hutool.cn/s#%E6%B5%8B%E8%AF%95", buildUrl);
}
Also used : UrlBuilder(cn.hutool.core.net.url.UrlBuilder) Test(org.junit.Test)

Example 45 with UrlBuilder

use of cn.hutool.core.net.url.UrlBuilder in project Jpom by dromara.

the class AutoRegSeverNode method autoPushToServer.

/**
 * 自动推送插件端信息到服务端
 *
 * @param url 服务端url
 */
public static void autoPushToServer(String url) {
    url = StrUtil.removeSuffix(url, CharPool.SINGLE_QUOTE + "");
    url = StrUtil.removePrefix(url, CharPool.SINGLE_QUOTE + "");
    UrlBuilder urlBuilder = UrlBuilder.ofHttp(url);
    // 
    LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address -> {
        // 非loopback地址,指127.*.*.*的地址
        return !address.isLoopbackAddress() && // 需为IPV4地址
        address instanceof Inet4Address;
    });
    Set<String> ips = localAddressList.stream().map(InetAddress::getHostAddress).filter(StrUtil::isNotEmpty).collect(Collectors.toSet());
    urlBuilder.addQuery("ips", CollUtil.join(ips, StrUtil.COMMA));
    AgentAuthorize agentAuthorize = AgentAuthorize.getInstance();
    urlBuilder.addQuery("loginName", agentAuthorize.getAgentName());
    urlBuilder.addQuery("loginPwd", agentAuthorize.getAgentPwd());
    int port = ConfigBean.getInstance().getPort();
    urlBuilder.addQuery("port", port + "");
    // 
    String build = urlBuilder.build();
    String body = HttpUtil.createGet(build).execute().body();
    Console.log("push result:" + body);
}
Also used : Inet4Address(java.net.Inet4Address) AgentAuthorize(io.jpom.system.AgentAuthorize) UrlBuilder(cn.hutool.core.net.url.UrlBuilder) InetAddress(java.net.InetAddress)

Aggregations

UrlBuilder (cn.hutool.core.net.url.UrlBuilder)45 Test (org.junit.Test)44 URI (java.net.URI)2 AgentAuthorize (io.jpom.system.AgentAuthorize)1 Inet4Address (java.net.Inet4Address)1 InetAddress (java.net.InetAddress)1