在 Apache HTTP 组件 4 类 org.apache.http.impl.auth.BasicScheme 中,我注意到该方法:
public static Header authenticate(
final Credentials credentials,
final String charset,
final boolean proxy)
已弃用,信息如下:
/**
* Returns a basic <tt>Authorization</tt> header value for the given
* {@link Credentials} and charset.
*
* @param credentials The credentials to encode.
* @param charset The charset to use for encoding the credentials
*
* @return a basic authorization header
*
* @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}.
*/
@Deprecated
但是,我没有看到任何文档说明如何从已弃用的功能迁移到新功能。尽管已弃用的功能有效,但我宁愿以“正确”的方式做事。以下是我如何使用已弃用的函数:
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
URI uriLogin = URI.create("http://localhost:8161/hawtio/auth/login/");
HttpPost hpLogin = new HttpPost(uriLogin);
hpLogin.setHeader(BasicScheme.authenticate(creds, "US-ASCII", false));
我如何将相同的概念应用于 BasicScheme.authenticate 的“正确”方法?
最佳答案
补充 oleg 的回答,这里有一个满足我需要的示例替换。
注意需要从静态调用转到对象实例。
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
URI uriLogin = URI.create("http://localhost:8161/hawtio/auth/login/");
HttpPost hpPost = new HttpPost(uriLogin);
Header header = new BasicScheme(StandardCharsets.UTF_8).authenticate(creds , hpPost, null);
hpPost.addHeader( header);
关于java - Apache HTTP BasicScheme.authenticate 已弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19797601/