我无法从java.net中找出Authenticator#requestPasswordAuthentication方法的实际用例。我知道这与请求身份验证的HttpClient有关,但不知道它是如何工作的。这与http基本身份验证有关吗?
我的第二个问题是requestPasswordAuthentication的方法参数是String host, InetAddress addr。既然我们已经指定了InetAddress,为什么还要指定host呢?Afaik,InetAddress用于封装ip地址。那么,如果我们已经指定了ip地址,为什么还需要host
发布于 2020-06-27 17:30:51
问题1的答案:如果需要密码,则存在关系。
问题2的答案:由于InetAddress对象是您的本地host.If您有多个IP地址,因此您可以决定绑定其中一个addresses.Similar Socket.bind(SocketAddress)
发布于 2020-06-27 22:10:14
我想出了这个测试用例来理解它:
@Test
void requestPasswordAuthentication() {
InetAddress inetAddress = InetAddress.getByName("localhost");
var upass = Authenticator.requestPasswordAuthentication(
inetAddress, -1, "", "", "");
assertEquals("uname", upass.getUserName());
assertEquals("password", String.valueOf(upass.getPassword()));
}https://stackoverflow.com/questions/62606385
复制相似问题