1
我正在尝试上传文件到使用HttpBuilder通过SSL运行的Web服务。我是从Gradle构建文件中完成的,但我不相信这真的很重要。该Web服务是Grails应用程序的一部分,但同样不要以为重要。使用HttpBuilder通过SSL上传文件
当您告诉它通过https运行时,我的grails应用程序使用由grails生成的证书在本地运行SSL。我的代码,本地工作如下:
def http = new groovyx.net.http.HTTPBuilder('https://localhost:8443/')
//=== SSL UNSECURE CERTIFICATE ===
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)
def httpsScheme = new Scheme("https", sf, 8443)
http.client.connectionManager.schemeRegistry.register(httpsScheme)
http.request(groovyx.net.http.Method.POST, groovyx.net.http.ContentType.JSON) { req ->
uri.path = '/a/admin/runtime/upload'
uri.query = [ username: "username", password: "password", version: version]
requestContentType = 'multipart/form-data'
org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity()
def file = new File("file.zip")
entity.addPart("runtimeFile", new org.apache.http.entity.mime.content.ByteArrayBody(file.getBytes(), 'file.zip'))
req.entity = entity
}
所有垃圾在上面是一些代码,我发现,让HttpBuilder自签名的证书工作。这实际上是在本地工作。 HttpBuilder文档说,大多数情况下,SSL应该“只是工作”。所以我用一个合法购买的认证,这样通过SSL代码如下:
def http = new groovyx.net.http.HTTPBuilder('https://www.realserver.com/')
http.request(groovyx.net.http.Method.POST, groovyx.net.http.ContentType.JSON) { req ->
uri.path = '/a/admin/runtime/upload'
uri.query = [ username: "username", password: "password" ]
requestContentType = 'multipart/form-data'
org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity()
def file = new File("file.zip")
entity.addPart("runtimeFile", new org.apache.http.entity.mime.content.ByteArrayBody(file.getBytes(), 'file.zip'))
req.entity = entity
}
当我运行此我得到以下错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
任何提示或建议,将不胜感激。
它不是自签名证书。正如我在我的问题中所说的,“这是通过SSL获得合法购买的认证”。 – Gregg
但是,此证书的根证书可能不在'cacerts'中,因此您必须导入它。 –