Httpclient 报 java.net.URISyntaxException 的错误

本文介绍了一种解决java.net.URISyntaxException错误的方法,通过修改HttpClient的DefaultRedirectStrategy.java中的createLocationURI方法,确保URL编码正确并避免非法字符引起的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



java.net.
URISyntaxException: Illegal character in query at index 44


下载 httpclient 源码 http://hc.apache.org/downloads.cgi

然后修改 httpcomponents-client-4.3.6\httpclient\src\main\java\org\apache\http\impl\client\DefaultRedirectStrategy.java 中的方法 createLocationURI

 1 import java.io.UnsupportedEncodingException;
 2 import java.net.URLEncoder;
 3 
 4 
 5 
 6 protected URI createLocationURI(final String location) throws ProtocolException {
 7         try {
 8             String url = URLEncoder.encode(location,"utf-8");
 9             final URIBuilder b = new URIBuilder(new URI(url).normalize());
10             //final URIBuilder b = new URIBuilder(new URI(location).normalize());
11             final String host = b.getHost();
12             if (host != null) {
13                 b.setHost(host.toLowerCase(Locale.ENGLISH));
14             }
15             final String path = b.getPath();
16             if (TextUtils.isEmpty(path)) {
17                 b.setPath("/");
18             }
19             return b.build();
20         } catch (final URISyntaxException ex) {
21             throw new ProtocolException("Invalid redirect URI: " + location, ex);
22         }catch(UnsupportedEncodingException ex){
23             throw new ProtocolException("Invalid redirect URI: " + location, ex);
24         }
25     }
View Code

mvn -Dtest -DfailIfNoTests=false package

编译好的文件下载 httpclient-4.3.6.jar.zip

转载于:https://www.cnblogs.com/JesseYang/p/4211201.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值