fix url bug

This commit is contained in:
Looly 2020-09-13 01:03:23 +08:00
parent c0c71a5f8f
commit ac5728d410
7 changed files with 55 additions and 8 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.4.3 (2020-09-11)
# 5.4.3 (2020-09-13)
### 新特性
* 【core 】 使用静态的of方法来new对象pr#177@Gitee
@ -12,6 +12,8 @@
* 【core 】 扩充Console功能支持可变参数issue#1077@Github
### Bug修复
* 【core 】 修复Dict.of错误issue#I1UUO5@Gitee
* 【core 】 修复UrlBuilder地址参数问题issue#I1UWCA@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -93,9 +93,9 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
String key = null;
for(int i = 0; i < keysAndValues.length; i++){
if(i % 2 == 0){
dict.put(key, keysAndValues[i]);
} else{
key = Convert.toStr(keysAndValues[i]);
} else{
dict.put(key, keysAndValues[i]);
}
}

View File

@ -70,6 +70,18 @@ public final class UrlBuilder implements Serializable {
return of(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getRawQuery(), uri.getFragment(), charset);
}
/**
* 使用URL字符串构建UrlBuilder当传入的URL没有协议时按照http协议对待<br>
* 此方法不对URL编码
*
* @param httpUrl URL字符串
* @return UrlBuilder
* @since 5.4.3
*/
public static UrlBuilder ofHttpWithoutEncode(String httpUrl) {
return ofHttp(httpUrl, null);
}
/**
* 使用URL字符串构建UrlBuilder当传入的URL没有协议时按照http协议对待
*

View File

@ -195,10 +195,10 @@ public class UrlQuery {
}
key = entry.getKey();
if (StrUtil.isNotEmpty(key)) {
sb.append(URLUtil.encodeAll(StrUtil.str(key), charset)).append("=");
sb.append(URLUtil.encodeAll(StrUtil.str(key), charset));
value = entry.getValue();
if (StrUtil.isNotEmpty(value)) {
sb.append(URLUtil.encodeAll(StrUtil.str(value), charset));
if (null != value) {
sb.append("=").append(URLUtil.encodeAll(StrUtil.str(value), charset));
}
}
}
@ -246,8 +246,8 @@ public class UrlQuery {
final String actualKey = URLUtil.decode(key, charset);
this.query.put(actualKey, StrUtil.nullToEmpty(URLUtil.decode(value, charset)));
} else if (null != value) {
// name为空value作为namevalue赋值""
this.query.put(URLUtil.decode(value, charset), StrUtil.EMPTY);
// name为空value作为namevalue赋值null
this.query.put(URLUtil.decode(value, charset), null);
}
}
}

View File

@ -30,4 +30,17 @@ public class DictTest {
Assert.assertEquals(1, dict.get("A"));
Assert.assertEquals(1, dict.get("a"));
}
@Test
public void ofTest(){
Dict dict = Dict.of(
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF"
);
Assert.assertEquals("#FF0000", dict.get("RED"));
Assert.assertEquals("#00FF00", dict.get("GREEN"));
Assert.assertEquals("#0000FF", dict.get("BLUE"));
}
}

View File

@ -1,5 +1,6 @@
package cn.hutool.core.net;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.net.url.UrlQuery;
import org.junit.Assert;
import org.junit.Test;
@ -16,4 +17,13 @@ public class UrlQueryTest {
Assert.assertEquals("111==", parse.get("b"));
Assert.assertEquals("a=1&b=111==", parse.toString());
}
@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);
}
}

View File

@ -133,4 +133,14 @@ public class HttpRequestTest {
HttpRequest request = HttpUtil.createGet("http://localhost:8888/get");
Console.log(request.execute().body());
}
@Test
@Ignore
public void getWithoutEncodeTest(){
String url = "https://img-cloud.voc.com.cn/140/2020/09/03/c3d41b93e0d32138574af8e8b50928b376ca5ba61599127028157.png?imageMogr2/auto-orient/thumbnail/500&pid=259848";
HttpRequest get = HttpUtil.createGet(url);
Console.log(get.getUrl());
HttpResponse execute = get.execute();
Console.log(execute.body());
}
}