Guest User

Untitled

a guest
May 27th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. package com.novell.webyast;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.Authenticator;
  6. import java.net.HttpURLConnection;
  7. import java.net.PasswordAuthentication;
  8. import java.net.URL;
  9.  
  10. public class RestClient {
  11.  
  12. // FIXME: JUnit this
  13. public String getMethod(final String scheme, final String hostname, final int port,
  14. final String resourcePath, final String user, final String pass)
  15. throws Exception {
  16. Authenticator.setDefault(new Authenticator() {
  17. protected PasswordAuthentication getPasswordAuthentication() {
  18. return new PasswordAuthentication(user, pass.toCharArray());
  19. }
  20. });
  21. HttpURLConnection c = (HttpURLConnection) new URL(scheme,
  22. hostname, port, resourcePath).openConnection();
  23. c.setUseCaches(false);
  24. c.connect();
  25.  
  26. BufferedReader in = new BufferedReader(new InputStreamReader(c
  27. .getInputStream()));
  28. String inputLine;
  29. StringBuilder sb = new StringBuilder();
  30. while ((inputLine = in.readLine()) != null)
  31. sb.append(inputLine + "\n");
  32. in.close();
  33. return sb.toString();
  34. }
  35. }
Add Comment
Please, Sign In to add comment