使用 Jersey 客户端的 HTTPS
如何使用 Jersey Client API 将 GET 请求发送到在 HTTPS 协议上运行的服务器。有我可以使用的示例代码吗?
How do I send GET requests using the Jersey Client API to a server which runs on the HTTPS protocol. Is there any sample code that I can use ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
构建您的客户端
从这篇博客文章中摘取更多详细信息:http://blogs.oracle.com /enterprisetechtips/entry/consuming_restful_web_services_with
有关设置证书的信息,请参阅这个很好回答的问题:在 Java 中将 HTTPS 与 REST 结合使用
Construct your client as such
Ripped from this blog post with more details: http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with
For information on setting up your certs, see this nicely answered SO question: Using HTTPS with REST in Java
如果您使用 java 6 ,7 和 8,则使用 Jersey 客户端的 HTTPS 有两个不同的版本
那么
如果使用 java 8 那么
请找到工作代码
POM
JAVA CLASS
HELPER CLASS
Helper 类
一旦开始运行应用程序将出现证书错误,从浏览器下载证书并添加到
添加到 cacerts 中,您将在以下链接中获得详细信息。
几乎没有有用的链接来理解错误
http: //www.9thirds.com/2015/01/restful-java-client-with-jersey-client.html
Java:sun.security.provider.certpath.SunCertPathBuilderException :无法找到请求目标的有效证书路径
http://magicmonster.com/kb/prg/java/ssl/ pkix_path_building_failed.html
导入证书后出现 Java Keytool 错误, “keytool错误:java.io.FileNotFoundException和访问被拒绝”
我已经使用 SSL 和基本身份验证测试了以下 get 和 post 方法的代码,您可以跳过 SSL 证书,您可以直接复制三个类并将 jar 添加到 java 项目中并运行。
访问
HTTPS using Jersey client has two different version if you are using java 6 ,7 and 8
then
If using java 8 then
Please find working code
POM
JAVA CLASS
HELPER CLASS
Helper class
Once you start running application will get Certificate error ,download certificate from browser and add into
Add into cacerts , you will get details in following links.
Few useful link to understand error
http://www.9threes.com/2015/01/restful-java-client-with-jersey-client.html
Java: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
http://magicmonster.com/kb/prg/java/ssl/pkix_path_building_failed.html
Java Keytool error after importing certificate , "keytool error: java.io.FileNotFoundException & Access Denied"
I have tested following code for get and post method with SSL and basic Authentication here you can skip SSL Certificate , you can directly copy three class and add jar into java project and run.
Jars
对于 Jersey 2,您需要修改代码:
https://gist.github.com/JAlexoid/b15dba31e5919586ae51
http://www.panz.in/2015/06/jersey2https.html
For Jersey 2 you'd need to modify the code:
https://gist.github.com/JAlexoid/b15dba31e5919586ae51
http://www.panz.in/2015/06/jersey2https.html
这里唤醒了一个死问题,但提供的答案不适用于 jdk 7(我在某处读到,Oracle 工程师对此存在一个错误,但尚未修复)。除了 @Ryan 提供的链接之外,您还必须添加:
(由许多 stackoverflow 答案组合在一起来解决这个问题)
完整的代码如下所示,这对我有用(如果不设置系统属性,客户端配置对我不起作用):
Waking up a dead question here but the answers provided will not work with jdk 7 (I read somewhere that a bug is open for this for Oracle Engineers but not fixed yet). Along with the link that @Ryan provided, you will have to also add :
(Courtesy to many stackoverflow answers combined together to figure this out)
The complete code will look as follows which worked for me (without setting the system property the Client Config did not work for me):
如果您使用的是 Java 8,Jersey2 的版本比 Aleksandr 提供的答案更短。
If you are using Java 8, a shorter version for Jersey2 than the answer provided by Aleksandr.