|
There
are two ways to do have your applet access a password protected
page. The first does not involve any Java code; the second is a
pure Java solution.
Since
Netscape caches the username and the password, you can let Netscape
do the authentication for you. It automatically be done if the
following conditions are met:
| |
The
user as already authenticated to access other pages on
the same server.
|
| |
|
| |
The
authentication challenge to access a page from the Applet
is exactly the same. This means that the host must be written
in the same way in both requests. (If one of them includes
a port number both requests must include a port number;
the realm should be the same.)
|
Assuming
that you are using the basic authentication scheme, the applet
includes the http authorization header itself. The value of the
header should be a BASIC authentication-string where the authentication-string
is a base64 encoding with the form user:password.
In
Sun's commerce API, there is a Base64 encoder class: javax.commerce.util.BASE64Encoder.
Here, you can either use that encoder class or write one yourself.
(All the information you need is found in RFC 1521). To ensure
that the browser actually sets the header, some output has to be
sent to the server (i.e., the request must be a POST request):
String userid;
String password;
URL cgiURL;
...
try {
URLConnection con = cgiURL.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty(("Authorization", MYBase64Encoder.encode(userid
+ ":" + password");
con.getOutputStream().close()
//We make a POST request even if we do not send data
InputStream in = con.getInputStream()//Continue as normal
} catch(IOException ioexc){
// Can mean that we did not supply a valid user-password pair
}
|