Created
December 16, 2018 13:16
-
-
Save bees4ever/9332ef913b597ced14c979c93f54a378 to your computer and use it in GitHub Desktop.
Scala download binary file from untrusted ssl url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// based on https://stackoverflow.com/a/28787883/5885054 with own modification | |
import javax.net.ssl._ | |
import java.security.cert.X509Certificate | |
import scala.io.Source | |
// Bypasses both client and server validation. | |
object TrustAll extends X509TrustManager { | |
val getAcceptedIssuers = null | |
def checkClientTrusted(x509Certificates: Array[X509Certificate], s: String) = {} | |
def checkServerTrusted(x509Certificates: Array[X509Certificate], s: String) = {} | |
} | |
// Verifies all host names by simply returning true. | |
object VerifiesAllHostNames extends HostnameVerifier { | |
def verify(s: String, sslSession: SSLSession) = true | |
} | |
// Main class | |
object Test extends App { | |
import sys.process._ | |
import java.net.URL | |
import java.io.File | |
// SSL Context initialization and configuration | |
val sslContext = SSLContext.getInstance("SSL") | |
sslContext.init(null, Array(TrustAll), new java.security.SecureRandom()) | |
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory) | |
HttpsURLConnection.setDefaultHostnameVerifier(VerifiesAllHostNames) | |
new URL("https://localhost:8443/api/posts/5c13debcd054b44db38dd709/image") #> new File("Output.jpg") !! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment