我有一个使用 HTTPBuilder 对 SSL 站点进行 RestAPI 调用的 groovy 脚本。要连接,我已将证书导入新的密钥库并使用 HTTPBuilder 支持页面 https://github.com/jgritman/httpbuilder/wiki/SSL中推荐的代码
这是代码:
def url = 'https://1.1.1.1'
def uri = new URIBuilder(url)
uri.path = '/rest/com/session'
def httpLOGIN = new HTTPBuilder(uri)
def keyStore = KeyStore.getInstance(KeyStore.defaultType)
getClass().getResource("\keystore.jks").withInputStream {
keyStore.load( it, "password".toCharArray())
}
String TempBasicAuth = "username:password".bytes.encodeBase64().toString()
httpLOGIN.client.connectionManager.schemeRegistry.register(
new Scheme("https", 443, new SSLSocketFactory(keyStore))
)
httpLOGIN.request(POST,JSON) { req ->
headers.'Authorization' = "Basic $TempBasicAuth"
response.failure = { resp, json ->
println "POST Failure. LOGIN: ${resp.statusLine}"
}
response.success = {// resp, json ->
println "POST Success. LOGIN: ${resp.statusLine}"*/
}
}
我得到的回应是
Caught: java.lang.NullPointerException: Cannot invoke method withInputStream() on null object
java.lang.NullPointerException: Cannot invoke method withInputStream() on null object
我尝试提供密钥库的完整路径,但得到相同的结果。是我没有正确提供路径(我尝试了多次迭代)还是还有其他问题?