post your comment   print   send to a friend
Rate:   0% | Views: 542
Question categories:  Java

How do I get my Applet to access a password protected page?

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

}

Customer Feedback
Rate:   0% | Views: 542 | Please Rate:  
 
If you have other comments or ideas for future technical tips, please type them here:

Email: (optional)

Comments: (optional)

 Dedicated Server | Web Designers    Back to serch results
Browse the Base
Knowledge Base
Web Design
  Do It Yourself
    Java
Messages
 

$75 Free Google AdWords

Free $75 Google AdWords when you sign up for WebImage! Target by location, create your own, or let Google create your ads for you. Check out http://www.aplus.net/google.html to see how AdWords works for you.

Private Area
 
Ask
in Private
   
Personal
Folder
 
Related Questions
 
1. How do I access Servlets or CGI programs from Java?
 
2. My applet computes its window size. Can I resize it in the browser?
 
3. How do I communicate between my applets in the same browser?
 
4. How do I force the browser to reload my Java classes without exiting Navigator?
 
5. Why does my java console show an applet is pruned?
 
Home Browse Search Ask in Private Personal Folder   Help
powered by web hosting 
  Logged as: Guest