Created
September 30, 2013 09:13
-
-
Save aya-eiya/6761231 to your computer and use it in GitHub Desktop.
ちょっとしたことがGroovy「エラーの出るSSL証明書を無視してHTTPSアクセスをテストする」 ref: http://qiita.com/aya_eiya/items/58ff3723a0f00574d421
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
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6') | |
import groovyx.net.http.* | |
import static groovyx.net.http.ContentType.* | |
import static groovyx.net.http.Method.* | |
import javax.net.ssl.X509TrustManager | |
import javax.net.ssl.SSLContext | |
import java.security.cert.X509Certificate | |
import javax.net.ssl.TrustManager | |
import java.security.SecureRandom | |
import org.apache.http.conn.ssl.SSLSocketFactory | |
import org.apache.http.conn.scheme.Scheme | |
import org.apache.http.conn.scheme.SchemeRegistry | |
def printlnErr = System.err.&println | |
def http = new HTTPBuilder( 'https://mySSLsite.hoge' ) | |
/* 俺俺証明書だとかのSSL証明書のエラーを無視する */ | |
def sslContext = SSLContext.getInstance("SSL") | |
sslContext.init(null, [ | |
new X509TrustManager(){ | |
public X509Certificate[] getAcceptedIssuers() {null} | |
public void checkClientTrusted(X509Certificate[] certs, String authType) { } | |
public void checkServerTrusted(X509Certificate[] certs, String authType) { } | |
}] as TrustManager[], new SecureRandom()) | |
def sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) | |
def httpsScheme = new Scheme("https", sf, 443) | |
http.client.connectionManager.schemeRegistry.register( httpsScheme ) | |
/* リクエスト */ | |
http.request( GET, JSON ) { | |
uri.path = "/admin" | |
response.success = { | |
println "done 200. ${uri.path}" | |
} | |
response.failure = {res -> | |
printlnErr "error ${res.statusLine.statusCode}. ${uri.path}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment