【问题标题】:HTTPBuilder - Cannot invoke method withInputStream() on null objectHTTPBuilder - 无法在空对象上调用方法 withInputStream()
【发布时间】:2018-01-11 06:08:24
【问题描述】:

我有一个使用 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

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

【问题讨论】:

  • 我怀疑您不想转义 "\keystore.jks" 中的“k”,所以如果您确实想要反斜杠,请使用 `\\` 转义反斜杠。

标签: groovy httpbuilder sslsocketfactory


【解决方案1】:

这个问题已经解释过了 -

Loading resources using getClass().getResource()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多