1

I want to use soap web service with this wsdl by java:

https://sadad.shaparak.ir/services/MerchantUtility.asmx?wsdl

but this error happens when I run some methods:

Failed to access the WSDL at: https://sadad.shaparak.ir/services/MerchantUtility.asmx?wsdl. It failed with: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

I use codes below to do this :

MerchantUtilitySoap port;
MerchantUtility service = new MerchantUtility();
port = service.getMerchantUtilitySoap();

And for example use this method from this web service:

long timeStamp = Long.parseLong(port.calcTimeStamp());

who can fix this problem? what should I do to fix it for my website?

Also I read these solutions but they can not help me to solve this problem:

javax.xml.ws.WebServiceException: Failed to access the WSDL

Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error?

"PKIX path building failed" and "unable to find valid certification path to requested target"

http://www.java-samples.com/showtutorial.php?tutorialid=210

Community
  • 1
  • 1
Atefeh Rashidi
  • 485
  • 1
  • 8
  • 32

1 Answers1

1

this is what I did to fix ValidatorException. First of all get the ceritificate (.cer file) of the website from your browser (because it's using HTTPS). Now, if you're on windows, open CMD and go to jre/bin folder and run this command:

keytool -importcert -file path/to/certificate.cer -keystore keystore.jks -alias "Alias"

-file : path of your .cer file which you got from browser

-alias : a name for this certificate

this command imports the certificate to keystore.jks file after you run, it asks for a password. set a password for keystore.jks. now, in your java code add these lines

System.setProperty("javax.net.ssl.trustStore","path/to/keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword","keystore_pass");

you can also copy the keystore.jks from jre/bin folder to your project folder and use it from there I think there are better ways but this is what I learnt from other stackoverflow topics and solved my problem.

Sina Nourian
  • 128
  • 10
  • thanks. But if I want to deploy my project on a host, what should I do? how can I write path of keystore.jks in java code?@Sina Nourian – Atefeh Rashidi Jun 05 '16 at 04:58
  • 1
    @AtefehRashidi You can put keystore file on your host. just put it in a folder somewhere and write the path. there should be no difference. for example, if you put keystore file near the code itself, path would be "keystore.jks". an example: your code is in "myhost/codes/mycode.java" and your keystore file is in "myhost/files/keystore.jks" your path to keystore from java code will be: "../files/keystore.jks". I only worked with PHP for server side programs and pointing to file is like what I said. pretty sure java is the same too. please test and let me know – Sina Nourian Jun 05 '16 at 06:03
  • In localhost it works but when I deploy my project on host, this error accure: javax.xml.ws.WebServiceException: Failed to access the WSDL It failed with: Got java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty while opening stream from @Sina Nourian – Atefeh Rashidi Jun 05 '16 at 08:17
  • 1
    @AtefehRashidi that means the truststore was not found or you don't have permission to access it. that's why it's working in localhost and not working on your server. also, write System.setProperty code before opening connection to https server. make sure the path you entered is valid and you have permission to read a file from the folder that you copied the keystore file in it. – Sina Nourian Jun 05 '16 at 08:48
  • I do this and put keystore file in true path and I write System.setProperty with true path in my java code before opening connection to https server. But It shows that exeption again.@Sina Nourian – Atefeh Rashidi Jun 05 '16 at 11:59
  • @AtefehRashidi Is your host linux or windows? – Sina Nourian Jun 05 '16 at 14:00
  • 1
    @AtefehRashidi I'm sorry I haven't work with Java on Linux. maybe you need to do some extra steps. there are some topics with "trustAnchors parameter must be non-empty" title on stackoverflow. please look for them maybe you find your answer there. also, if you find a solution for your new problem, post it here so others know how to fix it. – Sina Nourian Jun 06 '16 at 04:14