java - 有没有办法将非 ascii 字符转换为 unicode 并保持 ascii 不变?

标签 java unicode encoding uri apache-httpclient-4.x

我刚刚发现,如果 apache httpclient 包含百分比编码字母,它会返回错误解码的位置 header 。

enter image description here

当浏览器中的相同请求返回正确的字符串时:

enter image description here

我写了一个恢复uri的方法。我写对了吗?有没有更简单的方法?

import java.net.URLDecoder;

public class Test {
    public static void main(String[] args) throws Exception {
        String uri = "/search-zero?searchterm=\u00D1\u008C";
        String converted = convert(uri);
        System.out.println(converted); // /search-zero?searchterm=%D1%8C
        System.out.println(URLDecoder.decode(converted, "utf-8")); // /search-zero?searchterm=ь
    }

    private static String convert(String uri) {
        char[] chars = uri.toCharArray();
        int i = 0;
        StringBuilder result = new StringBuilder();
        while (i < chars.length) {
            int n = (int) chars[i];
            if (n > 127) {
                result.append('%');
                result.append(String.format("%02X", n));
            } else {
                result.append(chars[i]);
            }
            i++;
        }
        return result.toString();
    }
}

更新

我当前的 HttpClient 配置:

@Bean
public CloseableHttpClient getHttpClient() {
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(20);

    return HttpClients.custom()
            .setDefaultConnectionConfig(connectionConfig)
            .setConnectionManager(cm)
            .setRedirectStrategy(new CustomRedirectStrategy())
            .build();
}

public class CustomRedirectStrategy extends DefaultRedirectStrategy {

    @Override
    public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
        System.out.println(response.getFirstHeader("location"));
        URI uri = super.getLocationURI(request, response, context);
        return uri;
    }
}

工作代码(我们需要正确设置自定义连接管理器或删除它)感谢 OLEG!!

    @Bean
    public CloseableHttpClient getHttpClient() {
        ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();

//        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//        cm.setMaxTotal(200);
//        cm.setDefaultMaxPerRoute(20);

        return HttpClients.custom()
                .setDefaultConnectionConfig(connectionConfig)
//                .setConnectionManager(cm)
                .setRedirectStrategy(new CustomRedirectStrategy())
                .build();
    }

最佳答案

可以强制 HttpClient 对协议(protocol)元素使用非标准字符集,这应该可以提高与损坏的 Web 服务器的互操作性,这些服务器在“位置” header 中包含未转义的非 ASCII 字符

ConnectionConfig connectionConfig = ConnectionConfig.custom()
        .setCharset(Consts.ISO_8859_1)
        .build();
CloseableHttpClient client = HttpClients.custom()
        .setDefaultConnectionConfig(connectionConfig)
        .build();

关于java - 有没有办法将非 ascii 字符转换为 unicode 并保持 ascii 不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645286/

相关文章:

java - 获取网页的压缩版本

mysql - 无效的 unicode 字符导致 MySQL 字符串错误

javascript - 使用 javascript 解码转义的西里尔字母

php - 为什么 PHP str_replace 在我的服务器上不匹配某个字符串

java - 使用不带 LoadTimeWeaving 的服务自定义 UserDetails

java - ElasticSearch 多卷轴 Java API

java - 如何在 Hibernate 的 XML 文件中声明一对多映射?

php - 是否有易于使用且支持 unicode 的 PHP PDF 库?

unicode - Unicode 字符 MATHEMATICAL DOUBLE-STRUCK CAPITAL C 发生了什么?

java - 编码控制台俄语 sumbols 输入