Skip to content

Instantly share code, notes, and snippets.

@tomasherman
Created October 24, 2011 14:40
Show Gist options
  • Save tomasherman/1309188 to your computer and use it in GitHub Desktop.
Save tomasherman/1309188 to your computer and use it in GitHub Desktop.
Dispatch, Https and self signed certificate
/**
* Sorry about the mess in imports but i figured this in REPL and i didn't want to refactor it because i'm sure i would eff it up.
* First of all you need to import certificate into a keystore like so:
/opt/java/bin/keytool -import -alias ca -file ~/Desktop/www.ondrej.vostal.net -keystore cacerts
* This should create file castore. I'm not sure about the details of this but i'm sure it's fine ^_^
* This code is basically a ripoff of the http://stackoverflow.com/questions/5206010/using-apache-httpclient-for-https
*/
import dispatch._
import java.security._
import javax.net.ssl.TrustManagerFactory
import org.apache.http.conn.scheme.Scheme
import java.io._
val x = :/("www.httpsenabled-site.com")
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
val ks = KeyStore.getInstance("JKS")
val f = new java.io.File("pathtocacerts")
ks.load(new FileInputStream(f), null);
tmf.init(ks);
val sslContext = javax.net.ssl.SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(),null);
val sf = new org.apache.http.conn.ssl.SSLSocketFactory(sslContext)
val scheme = new Scheme("https", sf, 443);
Http.client.getConnectionManager.getSchemeRegistry.register(scheme)
Http(x.secure as_str) //<-- this should now work. I have no idea whether this is correct way to do things, but it seems to work. I would doublecheck it in production system, though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment