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

Java 通过在restlet中设置安全连接的凭据来获取json响应,java,authentication,https,restlet,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()

这是我能拥有的最简单的代码:

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
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
注意:

  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
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
  • 我可以在中找到URL() Firefox,然后要求输入用户名和密码。我把它装满,然后 获取json响应
  • 我还可以使用curl命令获得json响应

    curl-u kladhfklahglsgkagdkh:jgasdkjbvjkaggajkb-X GEThttps://www.myTestSite.com/requests

因此,它通过浏览器和curl工作,但不使用restlet

我的回复标题:

  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
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

我认为你的考试网站的证书是自签名的。在这种情况下,您需要用Java导入此证书。有关详细信息,请参阅此链接:

希望它能帮助您解决问题, 蒂埃里