HTTPBuilder - 无法在null对象上调用withInputStream()方法

时间:2018-01-11 06:08:25

标签: groovy httpbuilder sslsocketfactory

我有一个groovy脚本,使用HTTPBuilder对SSL站点进行RestAPI调用。要连接,我已将证书导入到新的密钥库中,并使用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

我已尝试提供密钥库的完整路径,但获得了相同的结果。是不是我没有正确提供路径(我已经尝试了多次迭代)还是有其他问题?

1 个答案:

答案 0 :(得分:0)

这个问题在这个问题中得到了解释 -

Loading resources using getClass().getResource()

getResource正在查找相对于类/脚本路径的路径,而不是文件路径。