Access a password protected URL (web application)

25 pts.
Tags:
Java code
java script
Greetings! I am facing an issue (class Authenticator was not called when the url asked for an username and password). Here is my sample code. Can anyone see what I am doing wrong or post their working sample code? My scenario, I think is little different from the the article, as I am trying to access a Web Application and it has a login screen, not the same Authentication popup dialog shown in this example. package HTMLParserSamples; import java.io.*; import java.net.*; import java.util.*; /** * Main.java * * @author www.javadb.com */ public class Authenticate { /** * Reads a web page into a StringBuilder object and prints it out to console * along with the size of the page. */ public void authenticate() { // Install the custom authenticator Authenticator.setDefault(new MyAuthenticator()); // Access the page try { // Create a URL for the desired page URL url = new URL("http://test.com/login.jsp"); // Read all the text returned by the server //BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); //urlc.setRequestProperty ("Authorization", "Basic " + encoding); urlc.setDoInput(true); urlc.setDoOutput(true); //urlc.setRequestMethod("GET"); urlc.setFollowRedirects(true); BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream()); StringBuilder builder = new StringBuilder(); int byteRead; while ((byteRead = buffer.read()) != -1) builder.append((char) byteRead); buffer.close(); System.out.println(builder.toString()); System.out.println("The size of the web page is " + builder.length() + " bytes."); } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } static class MyAuthenticator extends Authenticator { // This method is called when a password-protected URL is accessed protected PasswordAuthentication getPasswordAuthentication() { // Get information about the request String promptString = getRequestingPrompt(); String hostname = getRequestingHost(); InetAddress ipaddr = getRequestingSite(); int port = getRequestingPort(); // Get the username from the user... String username = "uname"; // Get the password from the user... String password = "passwd"; // Return the information return new PasswordAuthentication(username, password.toCharArray()); } } /** * Starts the program * * @param args * the command line arguments */ public static void main(String[] args) { new Authenticate().authenticate(); } }

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following