gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:51:37 27 4
gpt4 key购买 nike

我刚刚发现,如果 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com