简体   繁体   中英

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

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

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. The error is due to your Java application not trusting the target url's SSL cert when invoking the URL with HTTP Client. First get that installed using a java tool like http://opentox.ntua.gr/files/InstallCert.zip .

See http://www.opentox.org/tutorials/q-edit/how-to-install-ssl-certificates on how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM