Access a password protected URL using Java code

Lotus Domino
I am trying to access a URL using the HttpURLConnection in Java code. The URL is password protected. I have to authenticate our Lotus Domino server for accessing before even attempting to access the URL. How do I authenticate my Lotus Domino server to access the password protected URL using the HttpURLConnection? Can anyone give me any suggestions and/or explainations to accomplish this with some sample Java code?

Answer Wiki

Thanks. We'll let you know when a new response is added.

For a secure connection use the ‘HttpsURLConnection’ instead (Notice the extra S after http) as this is the normal way connections for passwords are made. Then just submit your request.
Your request will mostlikely need username and password parameters set and it shhould then connect you.

Discuss This Question: 1  Reply

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.
  • Mahamdevan
    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*; import*; import java.util.*; /** * * * @author */ 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(""); // 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 = != -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(); } }
    25 pointsBadges:

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.

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


Share this item with your network: