dromara / hutool

🍬A set of tools that keep Java sweet.
https://hutool.cn
Other
29.24k stars 7.53k forks source link

当表单中有null时,HttpUtil的urlWithFormUrlEncoded和toParams在格式化过程中都会丢内容 #3778

Closed zhangchaoxu closed 3 weeks ago

zhangchaoxu commented 3 weeks ago

版本情况

JDK版本: openjdk_8_201 hutool版本: 5.8.32(请确保最新尝试是否还有问题)

问题描述(包括截图)

  1. 复现代码
 @Test
    @DisplayName("urlencode")
    void  urlencode () {
        HashMap<String, Object> paramMap = new LinkedHashMap<>();
        paramMap.put("key", "123");
        paramMap.put("key1", null);
        paramMap.put("key2", "");
        paramMap.put("key3", "456");
        String url = HttpUtil.urlWithFormUrlEncoded("http://127.0.0.1/search", paramMap, Charset.defaultCharset());
        log.error(url);
        log.error(HttpUtil.toParams(paramMap, null, false));
        log.error(HttpUtil.toParams(paramMap, null, true));
        log.error(HttpUtil.toParams(paramMap, Charset.defaultCharset(), false));
        log.error(HttpUtil.toParams(paramMap, Charset.defaultCharset(), true));
    }
  1. 堆栈信息
    http://127.0.0.1/search?key=123&key1=key2=key3=456
    key=123&key1&key2=&key3=456
    key=123&key1&key2=&key3=456
    key=123&key1&key2=&key3=456
    key=123&key1&key2=&key3=456

    期望的结果应该是

    http://127.0.0.1/search?key=123&key1=&key2=&key3=456
    key=123&key1=&key2=&key3=456

比如报错的Excel文件,有问题的图片等。

zhangchaoxu commented 3 weeks ago

对于HttpUtil.toParams确实符合方法描述,能理解。 HttpUtil.urlWithFormUrlEncoded为什么会将&连接符丢了,有点疑惑

zhangchaoxu commented 3 weeks ago

问题了解了,是因为HttpUtil.normalizeParams对参数标准化的时候使用等号做分割,而toParams恰巧对null值不加等号,所以连接符&也就丢了