Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用非ascii凭据在httpclient 4.3.x中不起作用_Java_Character Encoding_Apache Httpclient 4.x - Fatal编程技术网

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

Java 使用非ascii凭据在httpclient 4.3.x中不起作用,java,character-encoding,apache-httpclient-4.x,Java,Character Encoding,Apache Httpclient 4.x,我查阅了httpclient 4.3.3 API,了解如何指定要用于头的字符集,以便包含用户名/密码的授权头可以使用特定的字符集,如UTF-8或iso-8859-1。用于此目的的不推荐使用的3.x API是 httpMethodInstance.getParams().setHttpElementCharset("iso-8859-1"); 我发现4.3.3中的等效API位于ConnectionConfig中。下面是我尝试使用的代码 HttpClientBuilder builder = Ht

我查阅了httpclient 4.3.3 API,了解如何指定要用于头的字符集,以便包含用户名/密码的授权头可以使用特定的字符集,如UTF-8或iso-8859-1。用于此目的的不推荐使用的3.x API是

httpMethodInstance.getParams().setHttpElementCharset("iso-8859-1");
我发现4.3.3中的等效API位于ConnectionConfig中。下面是我尝试使用的代码

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);
但在授权标头中发送的凭据的base64编码值表示未使用指定的字符集“iso-8859-1”对凭据进行编码。ConnectionConfig.setCharset()是设置http头字符集的正确方法吗?如果不是,那么4.3.x中不推荐使用的setHttpElementCharset()的正确等价物是什么


Apache mail archive指出这不容易支持,并建议使用BasicSchemeFactory,但我似乎不知道如何/在何处使用它来指定字符集。

自定义字符集编码适用于某些身份验证方案(如Basic和Digest),而不适用于其他方案。全局自定义auth字符集参数是个坏主意

必须使用自定义身份验证方案工厂按方案配置凭据字符集

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();
Lookup authSchemeRegistry=RegistryBuilder.create()
.register(AuthSchemes.BASIC,新的BasicSchemeFactory(Consts.UTF_8))
.register(AuthSchemes.DIGEST,新的DigestSchemeFactory(Consts.UTF_8))
.register(AuthSchemes.NTLM,新的NTLMSchemeFactory())
.register(AuthSchemes.SPNEGO,新的SPNegoSchemeFactory())
.register(AuthSchemes.KERBEROS,新的KerberosSchemeFactory())
.build();
CloseableHttpClient httpclient=HttpClients.custom()
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.build();