Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.security.KeyManagementException;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.security.cert.X509Certificate;
  10. import javax.net.ssl.HostnameVerifier;
  11. import javax.net.ssl.HttpsURLConnection;
  12. import javax.net.ssl.SSLContext;
  13. import javax.net.ssl.SSLSession;
  14. import java.util.Base64;
  15. import javax.net.ssl.TrustManager;
  16. import javax.net.ssl.X509TrustManager;
  17. import org.json.JSONObject;
  18. import org.json.XML;
  19.  
  20.  
  21. public class WebGet {
  22.  
  23. public static void disableCertificateValidation() throws MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException {
  24. // Create a trust manager that does not validate certificate chains
  25. TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
  26. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  27. return null;
  28. }
  29.  
  30. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  31. }
  32.  
  33. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  34. }
  35. }
  36. };
  37.  
  38. // Create all-trusting host name verifier
  39. HostnameVerifier allHostsValid = new HostnameVerifier() {
  40. public boolean verify(String hostname, SSLSession session) {
  41. return true;
  42. }
  43. };
  44.  
  45. // Install the all-trusting trust manager
  46. SSLContext sc = SSLContext.getInstance("SSL");
  47. sc.init(null, trustAllCerts, new java.security.SecureRandom());
  48. HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  49.  
  50. // Install the all-trusting host verifier
  51. HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  52.  
  53. String credentials = "user" + ":" + "psw";
  54. String encoding = Base64.getEncoder().encodeToString(credentials.getBytes("UTF-8"));
  55. URL url = new URL("someURL");
  56. URLConnection con = url.openConnection();
  57. con.setRequestProperty("Authorization", String.format("Basic %s", encoding));
  58. BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  59. StringBuilder sb = new StringBuilder();
  60. String apiResponse = "";
  61. while ((apiResponse = br.readLine()) != null) {
  62. sb.append(apiResponse);
  63. }
  64.  
  65. System.out.println(sb);
  66. JSONObject jsonObj = XML.toJSONObject(sb.toString());
  67. System.out.println("---------------------------");
  68. System.out.println(jsonObj);
  69. jsonObj.getJSONArray("devices");
  70.  
  71. }
  72.  
  73. public static void main(String[] args){
  74. disableCertificateValidation();
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement