Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.InputStreamReader;
  4. import java.net.Authenticator;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9.  
  10. import org.apache.commons.codec.binary.Base64;
  11.  
  12. public class ConnectToUrlUsingBasicAuthentication {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. try {
  17. String webPage = "http://servername/webservice/ABC.asmx/GetStudentReport";
  18. String name = "domain.lab\sachin";
  19. String password = "Password123";
  20.  
  21. //NtlmHandler handler = new NtlmHandler();
  22.  
  23. Authenticator.setDefault(new NtlmAuthenticator(name, password));
  24.  
  25. String authString = name + ":" + password;
  26. System.out.println("auth string: " + authString);
  27. byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
  28. String authStringEnc = new String(authEncBytes);
  29. System.out.println("Base64 encoded auth string: " + authStringEnc);
  30.  
  31. URL url = new URL(webPage);
  32. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  33. //conn.setRequestProperty("Authorization", "Basic " +authStringEnc );
  34. conn.connect();
  35.  
  36. System.out.println("Response Code: " + conn.getResponseCode() );
  37.  
  38. URLConnection urlConnection = url.openConnection();
  39. urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
  40. InputStream is = urlConnection.getInputStream();
  41. InputStreamReader isr = new InputStreamReader(is);
  42.  
  43. int numCharsRead;
  44. char[] charArray = new char[1024];
  45. StringBuffer sb = new StringBuffer();
  46. while ((numCharsRead = isr.read(charArray)) > 0) {
  47. sb.append(charArray, 0, numCharsRead);
  48. }
  49. String result = sb.toString();
  50.  
  51. System.out.println("*** BEGIN ***");
  52. System.out.println(result);
  53. System.out.println("*** END ***");
  54. } catch (MalformedURLException e) {
  55. e.printStackTrace();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. }
  62.  
  63. import java.net.Authenticator;
  64. import java.net.PasswordAuthentication;
  65.  
  66. public class NtlmAuthenticator extends Authenticator {
  67.  
  68. private final String username;
  69. private final char[] password;
  70.  
  71. public NtlmAuthenticator(final String username, final String password) {
  72. super();
  73. this.username = new String(username);
  74. this.password = password.toCharArray();
  75. }
  76.  
  77. @Override
  78. public PasswordAuthentication getPasswordAuthentication() {
  79. System.out.println("Scheme:" + getRequestingScheme() );
  80. System.out.println("Host:" + getRequestingHost() );
  81. PasswordAuthentication pa = new PasswordAuthentication(username, password);
  82. System.out.println("UserName:" + pa.getUserName() );
  83. System.out.println("Password:" + pa.getPassword().toString() );
  84. return pa;
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement