HttpClient 4.5 重定向到中文URL出现乱码的解决方案

一、问题描述

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

image


二、解决方案

问题的核心在于 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(
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值