我想在 Java 小程序中进行简单的 HTTP 身份验证,我正在尝试这种方式:
static class MyAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
// I haven't checked getRequestingScheme() here, since for NTLM
// and Negotiate, the usrname and password are all the same.
System.err.println("Feeding username and password for " + getRequestingScheme());
return (new PasswordAuthentication(kuser, kpass.toCharArray()));
}
}
public void paint(Graphics g) {
try
{
// String authenticate=Authenticator.getPasswordAuthentication();
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://Ip_address/jpg/image.jpg");
InputStream ins = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String str;
while((str = reader.readLine()) != null)
System.out.println(str);
//int s=2+2;
g.drawString("hello world", 50, 60 );
}
catch (Exception e)
{
System.out.println("Exception : "+ e);
}
}
但我在这一行遇到错误
Authenticator.setDefault(new MyAuthenticator());
例外是:
Exception : java.security.AccessControlException: access denied
(java.net.NetPermission setDefaultAuthenticator)
谁能告诉我现在该做什么,或者如何从 Java 小程序验证一个网站?