java.lang.IllegalArgumentException: Illegal character in query at index
程序员文章站
2024-02-14 17:06:16
...
问题描述:
java get 请求时,抛异常:java.lang.IllegalArgumentException: Illegal character in query at index 123
问题出现原因:
get请求参数中含特殊符号导致。例如:http://www.xxx.com/querycityid?city=北 京
参数中的空格导致了异常出现,当去掉参数中的空格时,无异常
解决:请求地址转成URI对象
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
String urlparams = url + "?" + queryparams;
//未修改之前
//reqMethod = RequestBuilder.get().setUri(urlparams).setConfig(requestConfig).build();
URL urlget = null;
try {
//修改后
urlget = new URL(urlparams);
URI uri = new URI(urlget.getProtocol(), urlget.getHost(), urlget.getPath(), urlget.getQuery(), null);
reqMethod = RequestBuilder.get().setUri(uri).setConfig(requestConfig).build();
} catch (MalformedURLException | URISyntaxException e) {
logger.error("{}",e.getMessage());
}
推荐阅读
-
爬坑记(1):配置地址有空格,java.lang.IllegalArgumentException: Illegal character in path at index 52
-
SpringCloud错误:Illegal character in path at index 29: http://localhost:7000/eureka/ /
-
java.lang.IllegalArgumentException: Illegal character in query at index
-
java.lang.IllegalArgumentException: Illegal charactor in path at index 0 \demo_index\_search
-
Skywalking Illegal character in hostname at index
-
java.lang.IllegalArgumentException: Illegal character in query at index
-
启动JBoss出现“java.net.URISyntaxException: Illegal character in path at index 11”解决
-
java8中使用java.util.Base64报“java.lang.IllegalArgumentException: Illegal base64 character d”
-
ERROR: Illegal character in opaque part at index 2: F:\Android\.android\debug.keystore
-
java.lang.IllegalArgumentException: Illegal character in query at index 141: