Advertisement
isurunix

REST API Call without SSL cert verification

Jul 5th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.05 KB | None | 0 0
  1. import groovy.json.JsonSlurper
  2.  
  3. import javax.net.ssl.HostnameVerifier
  4. import javax.net.ssl.HttpsURLConnection
  5. import javax.net.ssl.SSLContext
  6. import javax.net.ssl.TrustManager
  7. import javax.net.ssl.X509TrustManager
  8. import java.security.SecureRandom;
  9.  
  10. def apiKey = "api-key"
  11. def url = "https://somedomain.com/api/endpoint?private_token="+apiKey
  12.  
  13. //bypass SSL cert verification
  14. def sc = SSLContext.getInstance("SSL")
  15. def trustAll = [getAcceptedIssuers: {}, checkClientTrusted: { a, b -> }, checkServerTrusted: { a, b -> }]
  16. sc.init(null, [trustAll as X509TrustManager] as TrustManager[], new SecureRandom())
  17. hostnameVerifier = [verify: { hostname, session -> true }]
  18. HttpsURLConnection.defaultSSLSocketFactory = sc.socketFactory
  19. HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier as HostnameVerifier)
  20.  
  21. println url
  22.  
  23. //make REST call
  24. def response = new URL(url).openConnection() as HttpURLConnection
  25.  
  26. println response.responseCode
  27. if(response.responseCode.equals(200)){
  28.     responseBody = response.inputStream.text
  29.     println responseBody
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement