[英]How to pass security cookies into a HttpClient Get request (Apache Commons)
I need to invoke a GET request on a URL which requires authentication. 我需要在需要身份验证的URL上调用GET请求。 I am able to get around this when doing a curl by simply passing in values for the remember me and jSession cookies like so... 我可以通过简单地传递“记住我”和jSession cookie的值来解决此问题,就像这样...
curl -v -b "mysite_remember_me=MToxNDU5MzcwODg1MDU1Ojk0YmYwMjI1NDI5MTZZkMGM2NzRkMzkx;JSESSIONID=2FDB2480CD28D99147C281.app1" "www.mysite.com/doSomething/play/416?confirmed=false&contentType=2"
But when I try to replicate this in Java like so... 但是当我尝试像这样在Java中复制它时...
HttpMethod method = new GetMethod("www.mysite.com/doSomething/play/416?confirmed=false&contentType=2&source=sourceSku")
org.apache.commons.httpclient.Cookie cookie1 = new
org.apache.commons.httpclient.Cookie();
org.apache.commons.httpclient.Cookie cookie2 = new
org.apache.commons.httpclient.Cookie();
cookie1.setName("mysite_remember_me");
cookie1.setValue("MToxNDU5MzcwODg1MDU1Ojk0YmYwMjI1NDI5MTZZkMGM2NzRkMzkx");
cookie2.setName("JSESSIONID");
cookie2.setValue("2FDB2480CD28D99147C281.app1");
httpClient.getState().addCookie(cookie1);
httpClient.getState().addCookie(cookie2);
responseCode = httpClient.executeMethod(method)
I get this exception... 我得到这个例外...
Caught: 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
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
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1747)
The problem I believe is that the SSL certificate on the target server hasn't been installed into your JRE's cacerts file. 我认为问题是目标服务器上的SSL证书尚未安装到JRE的cacerts文件中。 The error is due to your Java application not trusting the target url's SSL cert when invoking the URL with HTTP Client. 该错误是由于您的Java应用程序在使用HTTP客户端调用URL时不信任目标URL的SSL证书。 First get that installed using a java tool like http://opentox.ntua.gr/files/InstallCert.zip . 首先使用诸如http://opentox.ntua.gr/files/InstallCert.zip之类的Java工具进行安装。
See http://www.opentox.org/tutorials/q-edit/how-to-install-ssl-certificates on how to do this. 有关如何执行此操作的信息,请参见http://www.opentox.org/tutorials/q-edit/how-to-install-ssl-certificates 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.