Wednesday, January 15, 2014

How to Get a Password Protected URL With Java



1. Paste the following Java program code into any word processor or text editor. The program displays a browser-like window, which you can enter a URL in.import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;public class GetProtectedURL extends Frame {private TextField field1 = new TextField();private TextArea area1 = new TextArea();public GetProtectedURL() {super ('Get Protected URL');// Make Authenticator//Authenticator call will go here// Layout the screenadd (field1, BorderLayout.NORTH);area1.setEditable(false);add (area1, BorderLayout.CENTER);field1.addActionListener (new ActionListener() {public void actionPerformed (ActionEvent e) {String s = field1.getText();if (s.length() != 0)area1.setText (getURLContent (s));}});addWindowListener (new WindowAdapter() {public void windowClosing (WindowEvent e) {dispose();System.exit(0);}});}private String getURLContent (String urlString) {StringWriter stringWriter1 = new StringWriter();PrintWriter printWriter1 = new PrintWriter(stringWriter1);try {URL url1 = new URL (urlString);InputStream content = (InputStream)url1.getContent();BufferedReader reader1 =new BufferedReader (new InputStreamReader (content));String contentLine;while ((contentLine = reader1.readLine()) != null) {printWriter1.println (contentLine);}} catch (MalformedURLException e) {printWriter1.println ('Invalid URL');} catch (IOException e) {printWriter1.println ('Error reading URL');}return stringWriter1.toString();}public static void main (String args[]) {Frame frame1 = new GetProtectedURL();frame1.setSize(300, 300);frame1.setVisible (true);}}

2. Save the program as 'GetProtectedURL.java,' with the file type set to 'Plain text.'

3. Open a DOS command prompt, then navigate to the folder containing the Java file you saved in the last step.

4. Compile the program by typing 'javac GetProtectedURL.java,' then run it by typing 'java GetProtectedURL.'

5. Enter the name of a non-password protected URL in the address text box. The Java browser will display the content. Enter a protected URL. The browser will display an error message. You'll resolve the error by creating an authenticator class in the next few steps.

6. Type, just before the last closing brace (i.e. '}') of the program listing, the following code, which creates the password authentication.class MyAuthenticator extends Authenticator {protected PasswordAuthentication getPasswordAuthentication() {String txtUsername = 'TYPE_YOUR_USERNAME_HERE';String txtPassword = 'TYPE_YOUR_PASSWORD_HERE';return new PasswordAuthentication (txtUsername, txtPassword.toCharArray() );} //method getPasswordAuthentication}

7. Type over the text 'TYPE_YOUR_USERNAME_HERE' the username your server requires to gain access to the protected content. Type over the text 'TYPE_YOUR_PASSWORD_HERE' the password your server needs.

8. Type, in place of the line '//Authenticator call will go here,' this code, which tells the JRE to call the code listed in the previous step, if the server you're requesting content from requires authentication:Authenticator.setDefault (new customAuthenticator ());

9. Recompile and run the program, with the password-protected URL you tried to enter in step 5. This time the browser will display the protected content.

No comments:

Post a Comment