Advertisement
Guest User

Untitled

a guest
May 11th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.Authenticator;
  8. import java.net.MalformedURLException;
  9. import java.net.PasswordAuthentication;
  10. import java.net.URL;
  11.  
  12. public class AuthDemo {
  13. public static void main(String args[]) throws MalformedURLException,
  14. IOException {
  15.  
  16. adres start = new adres();
  17. start.setVisible(true);
  18.  
  19.  
  20. String urlString = "";
  21. String username = "";
  22. String password = "";
  23. Authenticator.setDefault(new MyAuthenticator(username, password));
  24. URL url = new URL(urlString);
  25. InputStream content = (InputStream) url.getContent();
  26. BufferedReader in = new BufferedReader(new InputStreamReader(content));
  27. String line;
  28. while ((line = in.readLine()) != null) {
  29. System.out.println(line);
  30. }
  31. System.out.println("Done.");
  32. }
  33.  
  34. static class MyAuthenticator extends Authenticator {
  35. private String username, password;
  36.  
  37. public MyAuthenticator(String user, String pass) {
  38. username = user;
  39. password = pass;
  40. }
  41.  
  42. protected PasswordAuthentication getPasswordAuthentication() {
  43. System.out.println("Requesting Host : " + getRequestingHost());
  44. System.out.println("Requesting Port : " + getRequestingPort());
  45. System.out.println("Requesting Prompt : " + getRequestingPrompt());
  46. System.out.println("Requesting Protocol: "
  47. + getRequestingProtocol());
  48. System.out.println("Requesting Scheme : " + getRequestingScheme());
  49. System.out.println("Requesting Site : " + getRequestingSite());
  50. return new PasswordAuthentication(username, password.toCharArray());
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement