我的Java Desktop应用程序需要通过代理服务器来验证外部许可证服务器上的许可证。代理服务器使用带有Windows用户凭据的NTLM身份验证。 看谷歌,我在这里找到了一些代码。
System.setProperty("http.proxyHost", httpProxyAddress);
System.setProperty("http.proxyPort", httpProxyPort);
System.setProperty("https.proxyHost", httpProxyAddress);
System.setProperty("https.proxyPort", httpProxyPort);
System.setProperty("http.proxyUser", httpProxyUsername);
System.setProperty("http.proxyPassword", httpProxyPassword);
System.setProperty("https.proxyUser", httpProxyUsername);
System.setProperty("https.proxyPassword", httpProxyPassword);
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(httpProxyUsername, httpProxyPassword.toCharArray());
}
}
);
我可以通过以下代码获取httpProxyAddress和httpProxyPort:
try
{
System.setProperty("java.net.useSystemProxies","true");
List l = ProxySelector.getDefault().select(
new URI("http://www.yahoo.com/"));
for (Iterator iter = l.iterator(); iter.hasNext(); ) {
Proxy proxy = (Proxy) iter.next();
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress) proxy.address();
if(addr == null) {
System.out.println("No Proxy");
} else {
System.out.println("proxy hostname : " + addr.getHostName());
System.out.println("proxy port : " + addr.getPort());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
您能告诉我如何获取Windows用户凭据吗?在尝试验证许可证之前,我是否应该有一个弹出对话框询问用户凭据?
谢谢