Results 1 to 1 of 1
-
Reading URLs Protected with HTTP Authentication
This Java tip shows how to connect to a URL protected with HTTP Authentication.
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.net.URL; public class AuthDemo { public static void main(String args[]) throws MalformedURLException, IOException { String urlString = ""; String username = ""; String password = ""; Authenticator.setDefault(new MyAuthenticator(username, password)); URL url = new URL(urlString); InputStream content = (InputStream) url.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(content)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } System.out.println("Done."); } static class MyAuthenticator extends Authenticator { private String username, password; public MyAuthenticator(String user, String pass) { username = user; password = pass; } protected PasswordAuthentication getPasswordAuthentication() { System.out.println("Requesting Host : " + getRequestingHost()); System.out.println("Requesting Port : " + getRequestingPort()); System.out.println("Requesting Prompt : " + getRequestingPrompt()); System.out.println("Requesting Protocol: " + getRequestingProtocol()); System.out.println("Requesting Scheme : " + getRequestingScheme()); System.out.println("Requesting Site : " + getRequestingSite()); return new PasswordAuthentication(username, password.toCharArray()); } } }
Similar Threads
-
Integrated Windows Domian Authentication
By mahesh.komuravelli in forum Advanced JavaReplies: 1Last Post: 11-20-2009, 08:13 AM -
help with protected method in vector class
By katie in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 10:59 PM -
530 5.7.0 Authentication Required - JavaMail gmail
By simon in forum Advanced JavaReplies: 1Last Post: 07-14-2007, 11:52 PM -
JavaMail:Authentication required error
By bbq in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 04:16 AM -
WiKID Strong Authentication System 3.0.7
By levent in forum Java SoftwareReplies: 0Last Post: 05-16-2007, 04:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks