简体   繁体   English

使用非ASCII凭据在httpclient 4.3.x中不起作用

[英]Use of non-ascii credentials not working in httpclient 4.3.x

I looked up the httpclient 4.3.3 APIs on how to specify the charset to be used for headers so the Authorization header containing the username/password can use a specific charset such as UTF-8 or iso-8859-1. 我查找了httpclient 4.3.3 API,了解如何指定用于标头的字符集,以便包含用户名/密码的Authorization标头可以使用特定的字符集,例如UTF-8或iso-8859-1。 The deprecated 3.x API used for this is 为此使用的不推荐使用的3.x API是

httpMethodInstance.getParams().setHttpElementCharset("iso-8859-1");

The equivalent API in 4.3.3, I found is in ConnectionConfig. 我发现4.3.3中的等效API在ConnectionConfig中。 Following is the code I tried using 以下是我尝试使用的代码

HttpClientBuilder builder = HttpClientBuilder.create();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));

builder.setDefaultCredentialsProvider(credentialsProvider);

ConnectionConfig connectionConfig = ConnectionConfig.custom()
        .setCharset(Charset.forName("iso-8859-1")).build();
BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(
        registryBuilder.build());
connManager.setConnectionConfig(connectionConfig);
builder.setConnectionManager(connManager);



HttpHost target = new HttpHost(host, port, scheme);
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

CloseableHttpClient httpclient = builder.build();
CloseableHttpResponse response = httpclient.execute(target, request, localContext);

But the base64 encoded value of the credentials sent across in the authorization header indicates that the credentials are not encoded using the specified charset "iso-8859-1". 但是,在授权标头中发送的凭据的base64编码值表示凭据未使用指定的字符集“ iso-8859-1”进行编码。 Is ConnectionConfig.setCharset() the right method to use to set the http header charset? ConnectionConfig.setCharset()是用于设置http标头字符集的正确方法吗? If not, what is the correct equivalent of the deprecated setHttpElementCharset() in 4.3.x? 如果不是,则4.3.x中已弃用的setHttpElementCharset()的正确等效项是什么?

Apache mail archive http://mail-archives.apache.org/mod_mbox/hc-dev/201407.mbox/%3CJIRA.12727350.1405435834469.45355.1405437005945@arcas%3E indicates this is not readily supported and suggests the use of BasicSchemeFactory but I can't seem to figure out how/where to specify the charset using that. Apache邮件存档http://mail-archives.apache.org/mod_mbox/hc-dev/201407.mbox/%3CJIRA.12727350.1405435834469.45355.1405437005945@arcas%3E表示这不容易得到支持,并建议使用BasicSchemeFactory,但我可以似乎没有弄清楚如何/在哪里指定字符集。

Custom charset encoding is applicable to some auth schemes (such Basic and Digest) and not applicable to others. 自定义字符集编码适用于某些身份验证方案(例如基本和摘要),而不适用于其他身份验证方案。 Global custom auth charset parameter was a bad idea 全局自定义身份验证字符集参数是个坏主意

Credentials charset has to be configured on a per scheme basis using custom auth scheme factories 必须使用自定义身份验证方案工厂在每个方案的基础上配置凭据字符集

Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
            .register(AuthSchemes.BASIC, new BasicSchemeFactory(Consts.UTF_8))
            .register(AuthSchemes.DIGEST, new DigestSchemeFactory(Consts.UTF_8))
            .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
            .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
            .build();
CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultAuthSchemeRegistry(authSchemeRegistry)
        .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM