一、问题描述
遇到某个 URL A,请求时发现会重定向到某个包含了中文字符的 URL B。原以为只要 HttpClient 开启了自动重定向的功能,下载 A 指向的页面轻而易举,结果却出乎意料。HttpClient 在获取重定向后的 URL B 时出现了中文乱码,导致下载失败,具体报错信息见下图:

二、解决方案
问题的核心在于 ConnectionConfig 对象的 Charset 变量。如果你有使用到连接池,请参照如下方法:
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build());
如果你只是使用到 HttpClient 对象,那么可以参考以下方法:
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build())
.build();
三、过程分析
上面我直接给出了解决方案,有兴趣的话可以一起分析一下这个过程。
首先,我们要了解 HttpClient 在重定向的这个过程中做了什么。
默认情况下,HttpClient 的重定向策略依赖于 DefaultRedirectStrategy 这个类。该类的 getLocationURI(...) 方法用于获取重定向后的 URL,具体代码如下所示:
public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
Args.notNull(request, "HTTP request");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
HttpClientContext clientContext = HttpClientContext.adapt(context);
Header locationHeader = response.getFirstHeader("location");
if(locationHeader == null) {
throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location header");
} else {
String location = locationHeader.getValue();
if(this.log.isDebugEnabled()) {
this.log.debug("Redirect requested to location \'" + location + "\'");
}
RequestConfig config = clientContext.getRequestConfig();
URI uri = this.createLocationURI(location);
try {
if(!uri.isAbsolute()) {
if(!config.isRelativeRedirectsAllowed()) {
throw new ProtocolException("Relative redirect location \'" + uri + "\' not allowed");
}
HttpHost redirectLocations = clientContext.getTargetHost(

最低0.47元/天 解锁文章
2941

被折叠的 条评论
为什么被折叠?



