更正 HttpClient 4 中的基本身份验证。3.x

Correct Basic Authentication in HttpClient 4.3.x

我目前使用以下基本身份验证实现:

        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();
        ...

代码工作正常,但 BasicScheme.authenticate 已弃用。

在 HttpClient 4 中实现请求的基本身份验证的正确方法是什么。3.x?

以下对我有用 -

    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();

如果它适合你,请告诉我。