简体   繁体   English

在HttpClient 4.3.x中更正基本身份验证

[英]Correct Basic Authentication in HttpClient 4.3.x

I currently use the following basic authentication implementation: 我目前使用以下基本身份验证实现:

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(uri);

        // Set the basic authentication
        httpGet.addHeader(BasicScheme.authenticate(
                new UsernamePasswordCredentials("user", "pass"),
                "UTF-8", false));

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        ...

The code works fine however BasicScheme.authenticate is deprecated. 该代码可以正常工作,但是不建议使用BasicScheme.authenticate

What is the correct way to implement the basic authentication for a request in HttpClient 4.3.x? 在HttpClient 4.3.x中为请求实现基本身份验证的正确方法是什么?

The following works for me - 以下对我有用的作品-

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(uri);

    // Set the basic authentication
    String encoding = Base64Encoder.encode("user:pass");
    httpGet.setHeader("Authorization", "Basic" + encoding);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity entity = httpResponse.getEntity();

Please let me know if it works fine for you. 如果适合您,请告诉我。

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

相关问题 使用 4.3.x 重用 HttpClient 连接 - HttpClient connection reusing with 4.3.x 如何在httpClient 4.3.x中强制http客户端不自动处理身份验证挑战? - How to force the http client to not handle the authentication challenges automatically in httpClient 4.3.x? 使用HttpClient 4.3.x,为特定URL执行HttpHead会产生NoHttpResponseException - With HttpClient 4.3.x, executing a HttpHead for a specific URL gives NoHttpResponseException apache httpclient 4.4:HostnameVerifier从4.3.x过渡 - apache httpclient 4.4: HostnameVerifier transition from 4.3.x 使用非ASCII凭据在httpclient 4.3.x中不起作用 - Use of non-ascii credentials not working in httpclient 4.3.x HttpClient 4.3.x,修复不推荐使用的代码以使用当前的 HttpClient 实现 - HttpClient 4.3.x, fixing deprecated code to use current HttpClient implementations 带有Hibernate 4.3.x的BoneCP - BoneCP with Hibernate 4.3.x 获取Apache httpconents HttpClient 4.3.x OSGi包以解决Apache Karaf 2.3.x的问题 - Problems with getting Apache httpcomponents HttpClient 4.3.x OSGi bundle to work on Apache Karaf 2.3.x 有一种简单的方法可以手动强制缓存HttpClient 4.3.x绕过缓存吗? - Is there an easy way to manually force a caching HttpClient 4.3.x to bypass the cache? 如何使用Ajax从网页上使用httpclient 4.3.x获取信息 - how to use httpclient 4.3.x grabbing infomation from web page with ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM