gpt4 book ai didi

java - 正确的 HttpClient 4.3.x 中的基本身份验证

转载 作者:行者123 更新时间:2023-12-01 12:00:57 26 4
gpt4 key购买 nike

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

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

请告诉我它是否适合您。

关于java - 正确的 HttpClient 4.3.x 中的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27983175/

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