// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.net.*;
import java.io.*;

public class HttpGetAuth {
   public static void main(String[] args) {
      try {
         Authenticator.setDefault(new MyAuthenticator());
         URL url = new URL("http://www.xxxx.dk/prot2.html");
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = con.getInputStream();
            OutputStream os = new FileOutputStream("C:\\z.z");
            byte[] b = new byte[1000];
            int n;
            while ((n = is.read(b)) >= 0) {
               os.write(b, 0, n);
            }
            os.close();
            is.close();
         }
         con.disconnect();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

class MyAuthenticator extends Authenticator {
   protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("xxxx", "xxxx".toCharArray());
   }
}
