通过在restlet中设置安全连接的凭据来获取json响应

时间:2015-04-21 13:41:59

标签: java authentication https restlet

这是我能提供的最简单的代码:

ClientResource resource = new  ClientResource("https://www.myTestSite.com/requests");
resource.setChallengeResponse(ChallengeScheme.
HTTP_BASIC, "kladhfklahghlsgkagdkh", "jgasdkjbvjkagjhgajkb");
// Send the request
try {
resource.get();
} catch (ResourceException re) {
}
// Should be 200
System.out.println(resource.getStatus());

错误日志:

  Apr 21, 2015 7:04:01 PM org.restlet.engine.connector.HttpClientHelper start
    INFO: Starting the internal HTTP client
    Apr 21, 2015 7:04:04 PM org.restlet.resource.ClientResource retry
    INFO: A recoverable error was detected (1001), attempting again in 2000 ms.
    Apr 21, 2015 7:04:07 PM org.restlet.resource.ClientResource retry
    INFO: A recoverable error was detected (1001), attempting again in 2000 ms.
    Communication Error (1001) - The connector failed to complete the communication with the server
    Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    ... 32 more

注意:

  • 我可以点击中的网址(https://www.myTestSite.com/requests) 然后Firefox要求输入用户名和密码。我填写它和我 得到json的回应。
  • 我也可以使用curl命令获取json响应。

    curl -u kladhfklahghlsgkagdkh:jgasdkjbvjkagjhgajkb -X GET https://www.myTestSite.com/requests

所以它通过浏览器和卷曲工作但不使用restlet。

我的回复标题:

Status Code         : 200 OK
Content-Encoding    : gzip
Content-Type        : application/json; charset=utf-8
Date                : Tue, 21 Apr 2015 12:30:48 GMT
Etag                : W/"919-1189182981"
Server              : Apache
Vary                : X-HTTP-Method-Override

1 个答案:

答案 0 :(得分:0)

我认为您的测试网站的证书是自签名的。在这种情况下,您需要使用Java导入此证书。有关详细信息,请参阅此链接:http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/https

希望它可以帮助您并帮助您解决问题, 亨利